[英]WPF ContextMenu item style not applied in MVVM
我有一個基於 MVVM 的帶有上下文菜單的 WPF 應用程序。 上下文菜單項可以通過相應的視圖模型動態添加。 我的觀點是這樣的:
<Window x:Class="WpfApp11.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:WpfApp11"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type MenuItem}" x:Key="MyItemStyle">
<Setter Property="Header" Value="{Binding Header}"/>
</Style>
<HierarchicalDataTemplate x:Key="MyItemContainer"
DataType="{x:Type local:MenuItemViewModel}"
ItemsSource="{Binding MenuItems}">
</HierarchicalDataTemplate>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfApp11;component/Resources/Images.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Border BorderBrush="Blue" BorderThickness="3">
<Grid>
<Button Width="100" Height="30">
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding MeasurementContextMenu}"
ItemContainerStyle="{StaticResource MyItemStyle}"
ItemTemplate="{StaticResource MyItemContainer}"/>
</Button.ContextMenu>
</Button>
</Grid>
</Border>
</Window>
視圖模型如下所示:
public class MainModel : INotifyPropertyChanged
{
public MainModel()
{
Initialize();
}
private void Initialize()
{
var first = new MenuItemViewModel("Just a test");
var second = new MenuItemViewModel("Second");
first.MenuItems.Add(second);
MeasurementContextMenu.Add(first);
}
public ObservableCollection<MenuItemViewModel> MeasurementContextMenu { get; } = new ObservableCollection<MenuItemViewModel>();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
以及菜單項的視圖模型:
public class MenuItemViewModel : INotifyPropertyChanged
{
#region Fields
private string _header;
#endregion Fields
#region Constructors
public MenuItemViewModel(string header)
{
Header = header;
}
#endregion Constructors
#region Properties
public string Header
{
get
{
return _header;
}
set
{
if(_header != value)
{
_header = value;
OnPropertyChanged();
}
}
}
public ObservableCollection<MenuItemViewModel> MenuItems { get; } = new ObservableCollection<MenuItemViewModel>();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion Properties
}
所以分層的東西按預期工作,但應用的樣式不起作用。 所以項目出現但沒有任何文字。 這個屬性似乎沒有任何影響
ItemContainerStyle="{StaticResource MyItemStyle}"
在這種情況下如何應用樣式?
擺脫ItemContainerStyle
:
<ContextMenu ItemsSource="{Binding MeasurementContextMenu}"
ItemTemplate="{StaticResource MyItemContainer}"/>
...並在HierarchicalDataTemplate
放置一個綁定到Header
的TextBlock
:
<HierarchicalDataTemplate x:Key="MyItemContainer"
DataType="{x:Type local:MenuItemViewModel}"
ItemsSource="{Binding MenuItems}">
<TextBlock Text="{Binding Header}" />
</HierarchicalDataTemplate>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.