c# - 命令绑定(bind)在 Flyout 或 FlyoutMenu 中不起作用

标签 c# xaml uwp

因此,在相同的 View 和 View 模型中,我的其他按钮都以相同的方式装配,工作正常。

然而,这个恰好在上下文菜单中的菜单却没有。即使我将命令绑定(bind)更改为我知道不存在的东西,也没有任何绑定(bind)表达式错误,我觉得这非常有趣

XAML

  <ListView SelectionMode="Single" ItemsSource="{Binding Path=DisplayImages}" >
        <ListView.Resources>
           <Style TargetType="ListViewItem">
               <Setter Property="ContextFlyout">
                    <Setter.Value>
                         <MenuFlyout>
                            <MenuFlyoutItem Text="Save Image" Icon="Save" Command="{Binding Path=SaveImageCommand}"/>
                         </MenuFlyout>
                    </Setter.Value>
               </Setter>
            </Style>
  </ListView.Resources>

C#

public ICommand SaveImageCommand { get; set; }
SaveImageCommand = new CommandHandler(SaveImageExecuted, true);

private async void SaveImageExecuted()
{
}

我的命令处理程序

public class CommandHandler : ICommand
{
    private Action _action;
    private bool _canExecute;
    public CommandHandler(Action action, bool canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _action();
    }
}

最佳答案

所以我很确定这是框架中的错误。

为了解决这个问题,我决定只使用点击处理程序来调用 View 模型中执行的命令。 super 简陋,但它确实有效。

XAML

    <MenuFlyoutItem Text="Save Image" Icon="Save" Click="OnSaveContextMenuClicked"/>

代码背后

    private void OnSaveContextMenuClicked(object sender, RoutedEventArgs e)
    {
        viewModel.SaveImageCommand.Execute(sender);            
    }

关于c# - 命令绑定(bind)在 Flyout 或 FlyoutMenu 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554559/

相关文章:

c# - 将 Entity Framework Core 示例转换为 F#

Javascript - 通用 Windows 应用程序 - 捕获关闭事件

C# Diamond-Inheritance(接口(interface)实现)

c# - 反序列化具有字符串属性的 bool 元素

c# - Process.Kill() 需要什么权限?

c# - 代码隐藏中的 WPF DataGrid 样式

visual-studio-2010 - 如何通过键盘快捷键在vs2010中注释/取消注释XAML代码

c# - 来自 .NET 程序的 OpenGL 警告

xaml - 当我向 UWP ResourceDictionary 添加第二个对象时,未加载该资源

c# - 如何隐藏 AttachedFlyout 菜单的菜单项?