Wpf Dependency Properties

BigH61 581 Reputation points
2022-05-29T12:33:50.157+00:00

I am working on a UserControl and within this I have created some Dependency Properties in order to set a Header Text and Font however when I try to create one for FontSize it doesn’t work.
My Dependency Properties code for Font and FontSize are below.

Set Font (Working)

public FontFamily LeftHeaderFontFamily
        {
            get { return (FontFamily)GetValue(LeftHeaderFontFamilyProperty); }
            set { SetValue(LeftHeaderFontFamilyProperty, value); }
        }

        public static readonly DependencyProperty LeftHeaderFontFamilyProperty =
            DependencyProperty.Register("LeftHeaderFontFamily", typeof(FontFamily), typeof(BaseView),
                new PropertyMetadata(new FontFamily("Microsoft Sans Serif"), new PropertyChangedCallback(LeftHeaderFontChanged)));

        private static void LeftHeaderFontChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BaseView leftHeaderFont = d as BaseView;
            leftHeaderFont.LeftHeaderFontChanged(e);
        }PropertyCh

        private void LeftHeaderFontChanged(DependencyPropertyChangedEventArgs e)
        {
            LeftHeaderTextBlock.FontFamily = LeftHeaderFontFamily;
        }

Set FontSize (Not Working)

public double LeftHeaderFontSize
        {
            get { return (double)GetValue(LeftHeaderFontSizeProperty); }
            set { SetValue(LeftHeaderFontSizeProperty, value); }
        }

        public static readonly DependencyProperty LeftHeaderFontSizeProperty =
            DependencyProperty.Register("LeftHeaderFontSize", typeof(double), typeof(ViewBase),
                new PropertyMetadata(12D, new PropertyChangedCallback(LeftHeaderFontSizeChanged)));


        private static void LeftHeaderFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BaseView leftHeaderFontSize = d as BaseView;
            leftHeaderFontSize.LeftHeaderFontSizeChanged(e);
        }

        private void LeftHeaderFontSizeChanged(DependencyPropertyChangedEventArgs e)
        {
            LeftHeaderTextBlock.FontSize = LeftHeaderFontSize;
        }

The intention is that the Header will update at Design Time, however the FontSize Dependency Property doesn't work at all. I can make it work if I bind the control to the Dependency Property without the PropertyChangedCallback but this understandably doesn't update at DesignTime.
What am I doing wrong. Any assistance would be much appreciated.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2022-05-29T19:50:14.34+00:00

    Hi,
    I cannot reproduce your problem with your code without changes:

    <Window x:Class="WpfApp1.Window020"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="BigH61-8942_20220529" Height="450" Width="800">  
      <StackPanel>  
        <local:Window020UC1 LeftHeaderFontFamily="Times New Roman"   
                            LeftHeaderFontSize="{Binding ElementName=sl, Path=Value}"/>  
        <Slider x:Name="sl" Minimum="8" Maximum="128" Value="20"/>  
      </StackPanel>  
    </Window>  
    

    206448-x.gif

    0 comments No comments

0 additional answers

Sort by: Most helpful