关于wpf:ContextMenu命令绑定到父级和自身

ContextMenu Command Binding to parent and to self

我试图在具有多个命令绑定的DataGrid上创建ContextMenu。 一些必须绑定到本地ViewModel(即行的ViewModel),一些绑定到父项ViewModel。

到目前为止,我没有运气跟随其他解决方案。 我只能执行SubCommand。

MainViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MainViewModel : ViewModelBase
{
    public ObservableCollection<SubViewModel> Items { get; private set; }

    public MainViewModel()
    {

        Items = new ObservableCollection<SubViewModel>();

        Items.Add(new SubViewModel());
        Items.Add(new SubViewModel());
    }

    private RelayCommand _mainCommand;

    public ICommand MainCommand
    {
        get
        {
            if (_mainCommand == null)
            {
                _mainCommand = new RelayCommand(
                    () =>
                        {
                            Debug.WriteLine("MAINCOMMAND EXECUTED");
                        }
                    );
            }
            return _mainCommand;
        }
    }
}

SubViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class SubViewModel : ViewModelBase
{
    private RelayCommand _subCommand;

    public ICommand SubCommand
    {
        get
        {
            if (_subCommand == null)
            {
                _subCommand = new RelayCommand(
                    () =>
                        {
                            Debug.WriteLine("SUBCOMMAND EXECTUED");
                        }
                    );
            }
            return _subCommand;
        }
    }
}

主窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350"
        DataContext="{Binding Source={StaticResource Locator},
                              Path=Main}">
    <Grid>
        <DataGrid ItemsSource="{Binding Items}">

            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                                <MenuItem Command="{Binding DataContext.MainCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" Header="Main" />
                                <MenuItem Command="{Binding SubCommand}" Header="Sub" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>

        </DataGrid>
    </Grid>
</Window>


我找到了一种方法,但我认为它不太优雅,也许有更好的方法,但这样可行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350"
        DataContext="{Binding Source={StaticResource Locator},
                              Path=Main}">
    <Grid x:Name="Grid">
        <DataGrid x:Name="DataGrid" ItemsSource="{Binding Items}">

            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">

                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Command="{Binding Path=PlacementTarget.Tag.MainCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" Header="Main" />
                                <MenuItem Command="{Binding SubCommand}" Header="Sub" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>

                    <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext}" />
                </Style>
            </DataGrid.RowStyle>

        </DataGrid>
    </Grid>
</Window>