BigH61-8942 avatar image
0 Votes"
BigH61-8942 asked PeterFleischer-3316 edited

Wpf Dependency Properties

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;
         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;
         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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@BigH61-8942. What is the code of your BaseView? Is it possible to show the complete code that reproduces the problem for analysis?

0 Votes 0 ·

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered PeterFleischer-3316 edited

I cannot reproduce your problem with your code without changes:

 <Window x:Class="WpfApp1.Window020"
         Title="BigH61-8942_20220529" Height="450" Width="800">
     <local:Window020UC1 LeftHeaderFontFamily="Times New Roman" 
                         LeftHeaderFontSize="{Binding ElementName=sl, Path=Value}"/>
     <Slider x:Name="sl" Minimum="8" Maximum="128" Value="20"/>


x.gif (297.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.