带有默认模板和itemTemplate的递归菜单项

时间:2015-11-06 12:19:30

标签: wpf datatemplate menuitem controltemplate

我有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,但确实没有任何变化。

我该如何解决这个问题?

2 个答案:

答案 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作为项容器模板。导致MenuItemMenuItemCommandCommandProperty内部MenuItem,而样式位于外MenuItem }。这导致我的Command出现问题,因为外部MenuItem已经消耗了Click

现在使用ItemContainerStyle设置器应用于正确的MenuItem,而使用BasedOn属性会获取全局MenuItem样式的所有Setter。

答案 1 :(得分:0)

控制台,

  1. 为您的风格命名
  2. <Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}">
    
    1. 每次打算使用它时,请使用命名资源
    2.  <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>
      

      此致