我的xaml如下所示。我有一个可观察的Menus集合(称为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>
答案 0 :(得分: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>
使用 showIfTrue 和 hideIfTrue 一些转换器,它们采用布尔值并返回 Visibility.Visible 或如果布尔值为真,则分别为Visibility.Collapse 。
答案 1 :(得分: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>