我必须实现自定义Separator
,其中包含一些自定义属性(主要属性为Header
):
public class HeaderedSeparator : Separator
{
//More properties here... HeaderForeground and SeparatorHeight.
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(HeaderedSeparator));
[Bindable(true), Category("Common")]
public string Header
{
get { return (string)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
static HeaderedSeparator()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedSeparator), new FrameworkPropertyMetadata(typeof(HeaderedSeparator)));
}
}
Style
(在Themes \ Generic.xaml中):
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type c:HeaderedSeparator}">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="SeparatorHeight" Value="1"/>
<Setter Property="Foreground" Value="#FFE0E3E6"/>
<Setter Property="HeaderForeground" Value="#FF202326"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:HeaderedSeparator}">
<Grid Margin="0,4" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{TemplateBinding Header}" FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding HeaderForeground}"/>
<Rectangle Grid.Column="1" Height="{TemplateBinding SeparatorHeight}" Fill="{TemplateBinding Foreground}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Stretch="Fill"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(编辑)用法:
<c:HeaderedSeparator Header="Itens"/>
此分隔符应在菜单中使用时显示标题。这就是我使用Key
属性的原因,但由于某种原因,这不起作用(正在加载默认样式)。你们知道为什么吗?