2

我的 xml 如下所示。我有一个可观察的菜单集合(称为 Menulist),它又由一组称为 MenuItems 的 MenuItems 组成,我如何在 xaml 中创建分隔符样式

  <Menu Name="menu" DockPanel.Dock="Top" ItemsSource="{Binding MenuList}">
         <Menu.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=MenuItems}">
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding MenuClick}" />
                        <Setter Property="IsCheckable" Value="{Binding Checkable}" /> 
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImageSource}" />
                    <Label Content="{Binding Name}"/>
               </StackPanel>
            </HierarchicalDataTemplate>            
        </Menu.ItemTemplate>
       </Menu>
4

2 回答 2

1

您可以创建自定义分隔符样式,如下所示。

 <Style x:Key="MySeparatorStyle" TargetType="{x:Type Separator}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
        <Setter Property="Margin" Value="0,2,0,2"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Border 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        Height="1" 
                        SnapsToDevicePixels="true"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

使用这个。

 <Menu Name="menu" DockPanel.Dock="Top" ItemsSource="{Binding MenuList}">
         <Menu.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=MenuItems}">
               .......
            </HierarchicalDataTemplate>            
              <Separator Style={StaticResource MySeparatorStyle}" />
        </Menu.ItemTemplate>
       </Menu>
于 2013-06-13T19:27:53.660 回答
1

也许这种黑客(未经测试):

<Menu Name="menu" DockPanel.Dock="Top" ItemsSource="{Binding MenuList}">
 <Menu.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=MenuItems}">
        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Command" Value="{Binding MenuClick}" />
                <Setter Property="IsCheckable" Value="{Binding Checkable}" /> 
            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>
        <StackPanel Orientation="Horizontal" Visibility="{Binding IsSeparator,Converter=hideIfTrue}">
            <Image Source="{Binding ImageSource}" />
            <Label Content="{Binding Name}"/>
       </StackPanel>
       <Separator Visibility="{Binding IsSeparator,Converter=showIfTrue}" />
    </HierarchicalDataTemplate>            
  </Menu.ItemTemplate>
</Menu>

使用showIfTruehideIfTrue一些转换器采用布尔值并在布尔值为真时分别返回Visibility.VisibleVisibility.Collapse 。

于 2013-06-13T19:26:15.887 回答