Свойства шрифта пользовательского элемента управления WPF - PullRequest
0 голосов
/ 24 июня 2018

У меня есть два пользовательских элемента управления, один из которых получен из 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 ) );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...