ContextMenu Command Binding to parent and to self

别来无恙 提交于 2019-11-30 20:53:48

问题


I am trying to create a ContextMenu on a DataGrid with multiple Command Bindings. Some have to be bound to the local ViewModel (i.e. the ViewModel of the Row) and some to the parents ViewModel.

I had no luck so far following other solutions. I only get the SubCommand to execute.

MainViewModel

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

public class SubViewModel : ViewModelBase
{
    private RelayCommand _subCommand;

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

MainWindow

<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:


I have found a way, but I don't think it is too elegant, maybe there is a better way, but this will work:

<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>


来源:https://stackoverflow.com/questions/24781646/contextmenu-command-binding-to-parent-and-to-self

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!