给上下文菜单添加一个标题/标题

3

我希望为我的应用程序中的所有上下文菜单创建一个样式。这个样式将允许我向我的上下文菜单添加一个标题。我现在正在使用的样式如下:

<Style x:Uid="someStyle" TargetType="{x:Type ContextMenu}">
    <Setter x:Uid="Setter_240" Property="Background" Value="{DynamicResource someBrush}" />
    <Setter x:Uid="Setter_241" Property="BorderBrush" Value="{DynamicResource someBorderBrush}" />
    <Setter x:Uid="Setter_242" Property="SnapsToDevicePixels" Value="True" />
    <Setter x:Uid="Setter_243" Property="Template">
        <Setter.Value>
            <ControlTemplate x:Uid="ControlTemplate_30" TargetType="{x:Type ContextMenu}">
                <Grid x:Uid="Grid_27">
                    <Border x:Uid="Border_25" Margin="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
                    <StackPanel x:Uid="StackPanel_4" Background="{TemplateBinding Background}" IsItemsHost="True" ClipToBounds="True" Orientation="Vertical" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger x:Uid="Trigger_76" Property="IsEnabled" Value="False">
                        <Setter x:Uid="Setter_244" Property="Background" Value="{DynamicResource someBrush}" TargetName="Border" />
                        <Setter x:Uid="Setter_245" Property="BorderBrush" Value="{DynamicResource someBrush}" TargetName="Border" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这个样式对我来说已经非常好用,但是没有添加头部/标题的能力。我的初始想法是在这里放置一个虚拟标签。

<Grid x:Uid="Grid_27">
                <Border x:Uid="Border_25" Margin="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
                <Label Content {bind some way to allow the user to set it}>
                    <Label.Triggers>
                        <some way to hide the header if not set/>
                    <Label.Triggers>
                </Label>
                <StackPanel x:Uid="StackPanel_4" Background="{TemplateBinding Background}" IsItemsHost="True" ClipToBounds="True" Orientation="Vertical" />
            </Grid>

我希望我能够在所有上下文菜单中使用此标签作为可绑定的标题。 我遇到的问题是,我不知道如何使此标签可绑定,以便每个上下文菜单都可以具有不同的标题。 这个解决方案必须尽可能地自包含。 我不想让每个使用过上下文菜单的人都添加多行来启用此功能,也就是说,我希望标题是可选的而不是每个单独菜单的要求,因此如果存在触发器或其他可以在未设置标题的情况下隐藏标题的内容,那将是很好的。

最好/最干净的方法是什么,使其尽可能可重复使用?

1个回答

1
在你的样式中,只需向下传递绑定即可:
<Grid x:Uid="Grid_27" DataContext={Binding}>
            <Border .../>
            <Label Content="{Binding MenuTitle}" 
                   Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibility}}"/>
            ...the rest of your style...
        </Grid>

在使用中,提供数据上下文:
<MenuItem DataContext="{Binding SomeViewModel.Menu1}"
         Style={StaticResource yourstyle}>
  ...

其中Menu1是一个属性,其类型具有"MenuTitle"和"IsVisible"属性。


如果未设置数据上下文,会发生什么情况?有没有不需要 IsVisible 值的解决方案?触发器能否判断 MenuTitle 是否为空或 null 并且不显示标题? - Joseph Devlin
我相信如果你的数据上下文没有设置,你会在输出窗口中收到错误/警告。如果IsVisible是一个布尔值,那么你不需要设置它,它默认为“false”。如果你不想提供绑定,你可以通过样式中的setter提供默认值,这样样式的属性只会通过触发器改变。所以基本上,是的,绑定不是必需的。如果你不提供它,控件就不会显示任何东西(除非设置了默认值)。 - denis morozov
嗨,我使用了你的一些信息以及其他研究结果,最终得出了一个合适的解决方案。最终,我在我们的样式库中添加了第二个上下文菜单,用户必须通过 Style={StaticResource yourstyle} 指定他们想要使用的菜单。更改现有的上下文样式会导致其他地方的现有上下文菜单出现问题。谢谢。 - Joseph Devlin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接