【问题标题】:Add a header/title to a context menu将标题/标题添加到上下文菜单
【发布时间】:2013-05-11 09:40:07
【问题描述】:

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

<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>

我希望可以将此标签用作所有上下文菜单的可绑定标题/标题。我遇到的问题是我不知道如何使这个标签可绑定,以便每个上下文菜单都可以有不同的标题/标题。该解决方案还必须尽可能自包含。我不想让每个使用过上下文菜单的人都必须添加多行来启用此功能,即我希望标题是可选的,而不是每个菜单上的要求,所以如果有触发器或其他东西会很棒如果没有设置,可能会隐藏标题

实现这一目标并使其尽可能可重用的最佳/最干净的方法是什么?

【问题讨论】:

    标签: wpf xaml


    【解决方案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 还是空并且不显示标题吗?
    • 我相信如果您的数据上下文未设置,您将在输出窗口中收到错误/警告。如果 IsVisible 是一个布尔值,那么您不必设置它,默认为“false”。如果您不想提供绑定,则可以通过样式中的设置器提供默认值,这样样式的属性将仅通过触发器更改。所以基本上,是的,不需要绑定。如果您不提供,控件将不会显示任何内容(除非设置了默认值)
    • 嘿,我使用了您的一些信息以及其他一些研究来提出合适的解决方案。最后,我在样式库中选择了第二个上下文菜单,用户必须通过Style={StaticResource yourstyle} 指定他们想要使用的内容。更改现有的上下文样式会导致其他地方的现有上下文菜单出现问题。谢谢
    猜你喜欢
    • 2014-01-26
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2012-02-01
    相关资源
    最近更新 更多