我有两个自定义控件,一个自ContentPresenter派生,另一个自StackPanel派生,它们都不具有字体属性。现在,我发现我需要为它们实现字体属性。我曾尝试实现FrontFamily,FontWeigh和FontSize作为依赖项属性,但是它们没有出现在设计器中。有没有人和如何在自定义控件中实现字体的示例?
我的代码当前为:
[ Category( "Text" ) ]
public FontFamily FontFamily
{
get => (FontFamily)GetValue( FontFamilyProperty );
set => SetValue( FontFamilyProperty, value );
}
// Using a DependencyProperty as the backing store for FontFamily. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner( typeof( MyControl ), new FrameworkPropertyMetadata( SystemFonts.MessageFontFamily, FrameworkPropertyMetadataOptions.Inherits ) );
[ Category( "Text" ) ]
[ TypeConverter( typeof( FontSizeConverter ) ) ]
public double FontSize
{
get => (double)GetValue( FontSizeProperty );
set => SetValue( FontSizeProperty, value );
}
// Using a DependencyProperty as the backing store for FontSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner( typeof( MyControl ), new FrameworkPropertyMetadata( SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits ) );
[ Category( "Text" ) ]
public FontStretch FontStretch
{
get => (FontStretch)GetValue( FontStretchProperty );
set => SetValue( FontStretchProperty, value );
}
// Using a DependencyProperty as the backing store for FontStretch. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FontStretchProperty =
TextElement.FontStretchProperty.AddOwner( typeof( MyControl ), new FrameworkPropertyMetadata( TextElement.FontStretchProperty.DefaultMetadata.DefaultValue, FrameworkPropertyMetadataOptions.Inherits ) );
[ Category( "Text" ) ]
public FontStyle FontStyle
{
get => (FontStyle)GetValue( FontStyleProperty );
set => SetValue( FontStyleProperty, value );
}
// Using a DependencyProperty as the backing store for FontStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner( typeof( MyControl ), new FrameworkPropertyMetadata( SystemFonts.MessageFontStyle, FrameworkPropertyMetadataOptions.Inherits ) );
[ Category( "Text" ) ]
public FontWeight FontWeight
{
get => (FontWeight)GetValue( FontWeightProperty );
set => SetValue( FontWeightProperty, value );
}
// Using a DependencyProperty as the backing store for FontWeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner( typeof( MyControl ), new FrameworkPropertyMetadata( SystemFonts.MessageFontWeight, FrameworkPropertyMetadataOptions.Inherits ) );