如何将ContextMenu命令正确绑定到RelayCommand?

时间:2018-11-11 05:07:30

标签: c# .net wpf mvvm contextmenu

因此,我为RelayCommand设置了一些按钮,它工作正常,但是当尝试绑定Command的{​​{1}}属性时,它只是没有反应。 我读到一些有关必须设置级别的AncestorType的信息,但这是一个非常庞大的描述,没有说明原因或方式。

所以我有我的ListView

ContextMenu Menu Item

正如您在第二项上看到的那样,我正在尝试将其绑定到我的RelayCommand。 这应该可行,因为已经为我的按钮正确设置了视图模型和数据上下文。

<ListView x:Name="PlayerListView"
                  Width="200"
                  Height="330"
                  VerticalAlignment="Top"
                  Margin="0,80,15,0"
                  HorizontalAlignment="Right"
                  Background="#252525"
                  VerticalContentAlignment="Center"
                  ItemsSource="{Binding ServerViewModel.Players}">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" 
                                VerticalAlignment="Stretch" 
                                HorizontalAlignment="Stretch"
                                Width="190"
                                Background="#222222">

                        <StackPanel.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Command One">
                                    <MenuItem.Icon>
                                        <Image Source="../../Assets/image.png"
                                               RenderOptions.BitmapScalingMode="Fant"/>
                                    </MenuItem.Icon>
                                </MenuItem>

                                <MenuItem Header="Command Two"
                                          Command="{Binding ServerViewModel.MyCommand,
                                    RelativeSource={RelativeSource AncestorType=ListViewItem}}">
                                    <MenuItem.Icon>
                                        <Image Source="../../Assets/image.png"
                                               RenderOptions.BitmapScalingMode="Fant"/>
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </StackPanel.ContextMenu>

                        <Image Source="../../Assets/image.png"
                               Width="20"
                               Height="20"/>

                        <TextBlock Text="{Binding Username}" 
                                   Foreground="White"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Center" 
                                   Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

ViewModel

public class BaseViewModel : ObservableObject
    {
        public ServerViewModel ServerViewModel { get; set; } = new ServerViewModel();
    }

当然还有RelayCommand本身。 同样,RelayCommands适用于按钮,但不适用于ContextMenu项目

public RelayCommand MyCommand { get; set; }

public ServerViewModel()
        {
            MyCommand = new RelayCommand(DoSomething);
        }
public void DoSomething(object parameter)
        {
            MessageBox.Show("Working!");
        }

这是我设置DataContext的地方

public class RelayCommand : ObservableObject, ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        public RelayCommand(Action<object> execute) : this(execute, null)
        {

        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute.Invoke(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }

1 个答案:

答案 0 :(得分:1)

此代码

<com.example.saidi.custombottobar.view.ButtomSheetCustom
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="200dp"
        android:background="@color/colorAccent"
        tools:context=".view.MainActivity">

尝试在ListViewItem类中找到属性ServerViewModel,但找不到它。 如果ServerViewModel.Players返回的对象本身是ViewModel对象,则可以删除RelativeSource部分,然后将命令添加到Player ViewModel。

示例:

Command="{Binding ServerViewModel.MyCommand, RelativeSource={RelativeSource AncestorType=ListViewItem}}"

以及在PlayerViewModel中:

Command="{Binding MyCommand}"

有关其他解决方案,请参见此处:WPF: Binding a ContextMenu to an MVVM Command