我有MenuItem
类型的WPF控件模板:
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Cursor"
Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border Background="{TemplateBinding Background}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentControl Content="{TemplateBinding Header}"
Margin="5"
Grid.Column="1" />
<Path Grid.Column="2"
x:Name="Indicator"
Data="M1,1 L1,9 9,5Z"
Fill="{StaticResource GlyphBrush}"
Margin="4"
Visibility="Hidden"
VerticalAlignment="Center" />
<Popup Name="PART_Popup"
Placement="Right"
IsOpen="{TemplateBinding IsSubmenuOpen}"
AllowsTransparency="True"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalOffset="3"
VerticalOffset="-1">
<Border Background="Transparent">
<ContentControl Style="{StaticResource PopupContentStyle}">
<ItemsPresenter/>
</ContentControl>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="true">
<Setter Property="Background"
Value="{StaticResource ButtonHoverBackgroundBrush}" />
</Trigger>
<Trigger Property="IsChecked"
Value="false">
<Setter Property="Background"
Value="{StaticResource BackgroundBrush}" />
</Trigger>
<Trigger Property="HasItems"
Value="True">
<Setter TargetName="Indicator"
Property="Visibility"
Value="Visible" />
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Opacity"
Value="{StaticResource DisabledTransparency}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="{StaticResource ButtonHoverBackgroundBrush}" />
</Trigger>
</Style.Triggers>
</Style>
如果我现在将项目添加到MenuItem
,则模板可以完美运行。但是,如果我尝试将项目添加到绑定ObservableCollection
,则会遇到问题,因为我的ControlTemplate
未在任何地方使用原始ItemTemplate
的{{1}}。
此用法会导致问题:
MenuItem
<MenuItem Header="{userInterface:Translation Language}"
ItemsSource="{Binding AvailableLanguages}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ChangeLanguageCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Header}"
Header="{Binding}" />
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
不使用我在上面指定的MenuItem
,而是再次使用我的默认模板。
我尝试将全局模板的Template
更改为ItemsPresenter
,但确实没有任何变化。
我该如何解决这个问题?
答案 0 :(得分:1)
我通过将ItemTemplate
更改为ItemContainerStyle
来解决此问题:
这是结果:
<MenuItem Header="{userInterface:Translation Language}"
ItemsSource="{Binding AvailableLanguages}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header"
Value="{Binding Name}" />
<Setter Property="Command"
Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ChangeLanguageCommand}" />
<Setter Property="CommandParameter"
Value="{Binding RelativeSource={RelativeSource Self}, Path=DataContext}" />
<Setter Property="IsChecked"
Value="{Binding IsSelected}">
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
初始ItemTemplate
的问题是MenuItem
内部使用MenuItem
作为项容器模板。导致MenuItem
内MenuItem
内Command
和CommandProperty
内部MenuItem
,而样式位于外MenuItem
}。这导致我的Command
出现问题,因为外部MenuItem
已经消耗了Click
。
现在使用ItemContainerStyle
设置器应用于正确的MenuItem
,而使用BasedOn
属性会获取全局MenuItem
样式的所有Setter。
答案 1 :(得分:0)
控制台,
<Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}">
<MenuItem Header="Hello"
ItemsSource="{Binding AvailableLanguages}"
Style="{DynamicResource MenuItemStyle1}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Style="{DynamicResource MenuItemStyle1}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ChangeLanguageCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Header}">
<MenuItem.Header>
<TextBlock Text="Hello" />
</MenuItem.Header>
</MenuItem>
</DataTemplate>
</MenuItem.ItemTemplate>
此致