wpf binding datagrid viewmodel

wpf - ContextMenu Command Binding para padres y para uno mismo



datagrid viewmodel (1)

He encontrado una manera, pero no creo que sea demasiado elegante, tal vez haya una mejor manera, pero esto funcionará:

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

Estoy intentando crear un ContextMenu en un DataGrid con múltiples Enlaces de Comandos. Algunos tienen que estar vinculados al ViewModel local (es decir, el ViewModel de la fila) y algunos a los padres ViewModel.

Hasta ahora no he tenido suerte siguiendo otras soluciones. Solo obtengo el Subcomando para ejecutar.

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

Ventana principal

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