如何绑定到 WPF MVVM 中的上下文 MenuItem header?
How do I bind to a context MenuItem header in WPF MVVM?
我正在尝试按照 this answer 了解如何在 WPF 中制作 MVVM 上下文菜单。听起来很简单:"The ItemTemplate for the contextmenu items can now access the name, the command and whatever else you might need."
没有提及更改数据上下文、可视化树等
这是我的 ViewModel:
public class ViewModel
{
public class ContextAction : INotifyPropertyChanged
{
public string HeaderText;
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public ObservableCollection<ContextAction> ContextMenuActions { get; set; }
public ViewModel()
{
ContextMenuActions = new ObservableCollection<ContextAction>();
ContextMenuActions.Add(new ContextAction { HeaderText = "Foo" });
ContextMenuActions.Add(new ContextAction { HeaderText = "Bar" });
ContextMenuActions.Add(new ContextAction { HeaderText = "Baz" });
}
}
...还有我的 XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid Background="red">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding ContextMenuActions}">
<ContextMenu.ItemTemplate >
<DataTemplate DataType="MenuItem">
<MenuItem Header="{Binding HeaderText}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
我可以看到项目已添加到上下文菜单中。 Right-clicking 在网格上生成一个包含三个空白项的菜单。但是,header 绑定不起作用(每个菜单项都是空白的)。我是否遗漏了我链接的答案中的某些内容?我需要制作某种代理 class as mentioned here 吗?对于制作上下文菜单这样的简单任务来说,这似乎相当复杂,甚至在我链接到的第一个答案中都没有暗示。
您只能绑定到 属性,不能绑定到 字段。
public string HeaderText {get; set;}
我正在尝试按照 this answer 了解如何在 WPF 中制作 MVVM 上下文菜单。听起来很简单:"The ItemTemplate for the contextmenu items can now access the name, the command and whatever else you might need."
没有提及更改数据上下文、可视化树等
这是我的 ViewModel:
public class ViewModel
{
public class ContextAction : INotifyPropertyChanged
{
public string HeaderText;
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public ObservableCollection<ContextAction> ContextMenuActions { get; set; }
public ViewModel()
{
ContextMenuActions = new ObservableCollection<ContextAction>();
ContextMenuActions.Add(new ContextAction { HeaderText = "Foo" });
ContextMenuActions.Add(new ContextAction { HeaderText = "Bar" });
ContextMenuActions.Add(new ContextAction { HeaderText = "Baz" });
}
}
...还有我的 XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid Background="red">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding ContextMenuActions}">
<ContextMenu.ItemTemplate >
<DataTemplate DataType="MenuItem">
<MenuItem Header="{Binding HeaderText}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
我可以看到项目已添加到上下文菜单中。 Right-clicking 在网格上生成一个包含三个空白项的菜单。但是,header 绑定不起作用(每个菜单项都是空白的)。我是否遗漏了我链接的答案中的某些内容?我需要制作某种代理 class as mentioned here 吗?对于制作上下文菜单这样的简单任务来说,这似乎相当复杂,甚至在我链接到的第一个答案中都没有暗示。
您只能绑定到 属性,不能绑定到 字段。
public string HeaderText {get; set;}