0

I have implement these codes, but this seems doesn't work when I tried to change the variable (TitleBarHeight) value. Please check:

Variables.cs:

namespace MyApp.Classes
{
    public class Variables : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private static double _TitleBarHeight = 40;
        public static double TitleBarHeight
        {
            get { return _TitleBarHeight; }
            set
            {
                _TitleBarHeight = value;
                PropertyChanged(); //this line can't be added because it's non-static.
            }
        }
    }
}

MainPage.cs:

...
        Classes.Variables.TitleBarHeight = coreTitleBar.Height;
...

MainPage.xaml:

    <Page
      x:Class="MyApp.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:MyApp"
      xmlns:var="using:MyApp.Classes"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

      <Grid>
         <TextBlock Text="{x:Bind var:Variables.TitleBarHeight, Mode=OneWay}"/>
      </Grid>
   </Page>

In this case, I need to use static variable. Are there any mistakes?

wonyh_
  • 132
  • 1
  • 9
  • [This topic](https://stackoverflow.com/questions/936304/binding-to-static-property) may help i guess. You can tak – Alp Jan 17 '22 at 12:50
  • OK, this marked answer solved my problem: https://stackoverflow.com/questions/14614190/inotifypropertychanged-and-static-properties – wonyh_ Jan 18 '22 at 00:25

1 Answers1

0

I don't think you can raise change notifications for a static property from your Variables class, but since you seem to update the TitleBarHeight property in the MainPage you could simply call Bindings.Update to refresh the binding:

Classes.Variables.TitleBarHeight = coreTitleBar.Height;
Bindings.Update();

The other option is to wrap your static property in a class that implements INotifyPropertyChanged (like for example the MainPage):

public static double WrappedTitleBarHeight
{
    get { return Classes.Variables.TitleBarHeight; }
    set
    {
        Classes.Variables.TitleBarHeight = value;
        PropertyChanged();
    }
}

...and bind to the wrapper:

<TextBlock Text="{x:Bind WrappedTitleBarHeight, Mode=OneWay}"/>
mm8
  • 163,881
  • 10
  • 57
  • 88