ContextMenu 命令绑定到父级和自身 [英] ContextMenu Command Binding to parent and to self
本文介绍了ContextMenu 命令绑定到父级和自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在具有多个命令绑定的 DataGrid 上创建一个 ContextMenu.有些必须绑定到本地 ViewModel(即行的 ViewModel),有些必须绑定到父 ViewModel.
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.
到目前为止,我没有遵循其他解决方案.我只让 SubCommand 执行.
I had no luck so far following other solutions. I only get the SubCommand to execute.
主视图模型
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;
}
}
}
子视图模型
public class SubViewModel : ViewModelBase
{
private RelayCommand _subCommand;
public ICommand SubCommand
{
get
{
if (_subCommand == null)
{
_subCommand = new RelayCommand(
() =>
{
Debug.WriteLine("SUBCOMMAND EXECTUED");
}
);
}
return _subCommand;
}
}
}
主窗口
<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>
推荐答案
我已经找到了一个方法,但我认为它不是太优雅,也许有更好的方法,但是这个方法行得通:
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>
这篇关于ContextMenu 命令绑定到父级和自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文