gpt4 book ai didi

wpf - ContextMenu 命令绑定(bind)到父级和自身

转载 作者:行者123 更新时间:2023-12-02 21:25:25 24 4
gpt4 key购买 nike

我正在尝试在具有多个命令绑定(bind)的 DataGrid 上创建一个上下文菜单。有些必须绑定(bind)到本地 ViewModel(即 Row 的 ViewModel),有些必须绑定(bind)到父 ViewModel。

到目前为止,我没有遵循其他解决方案。我只执行子命令。

主视图模型

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

subview 模型

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>

最佳答案

我找到了一种方法,但我认为它不太优雅,也许有更好的方法,但这会起作用:

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

关于wpf - ContextMenu 命令绑定(bind)到父级和自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com