wpf - 具有默认模板和 itemTemplate 的递归菜单项

标签 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 中,模板将完美运行。但是,如果我尝试将项目添加到绑定(bind)的 ObservableCollection 中,我会遇到问题,因为我的 ControlTemplate 不使用原始 ItemTemplate 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 更改为 ItemsControl,但它没有改变任何内容。

如何解决这个问题?

最佳答案

我通过将 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,其中 CommandCommandProperty 位于内部 MenuItem 上 而样式位于外部 MenuItem 上。这导致我的 Command 出现问题,因为外部 MenuItem 已经消耗了 Click

现在,使用 ItemContainerStyle 时,setter 会应用于正确的 MenuItem,而使用 BasedOn 属性则采用全局 的所有 Setters MenuItem 样式。

关于wpf - 具有默认模板和 itemTemplate 的递归菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33566668/

相关文章:

c# - 在 WPF 或 Silverlight 中单击透明图像

c# - 将数据绑定(bind)到 busyindicator 中的数据模板的问题

c# - 在 DrawingContext 上绘制时在图像上绘制阴影

c# - MessageBox.Show 导致无法拾取光标的变化

WPF - 为以编程方式添加的 GridViewColumns 设置 DataTemplate

delphi - 如何添加在 OSX 上按预期工作的菜单项分隔符?

c# - XAML - C# - MenuItem 中的图片未显示

wpf - 从 DataTemplate 内的资源绑定(bind)

wpf - 将 CommandTarget 设置为 TabControl 中的选定控件

android - 禁用时 ActionBar 中的半透明图标 : only on my phone?