简体   繁体   中英

How do I bind FontSize for WPF TextBox in XAML to a class member variable?

How do I bind FontSize for WPF TextBox in XAML to a class member variable?

I have a collection of fonts that I use through the application. I would like to change the values of those fonts dynamically in my code behind and then have the changes reflected during runtime.

How do I achieve this?

Here is what my class definition looks like

public ClassFoo
{
   public double FontSize {get; set;}
}

This is how I define my class in MainWindow.xaml.cs:

public ClassFoo SampleClass;

Here is my what my XAML looks like:

<TextBlock Name="txtSample" Text="SomeText" 
     FontSize="{Binding SampleClass.FontSize}"/>

Then at runtime, I instantiate the class and initialize it:

SampleClass = new ClassFoo()
{
   FontSize = 16;
}

I would create it like that:

 public class MainWindow : Page
    {
        public Foo Foo { get; set; }

        public MainWindow()
        {
            DataContext = this;
        }
    }


    public class Foo : INotifyPropertyChanged
    {
        private double _fontSize;

        public double FontSize
        {
            get { return _fontSize; }
            set
            {
                _fontSize = value;
                OnPropertyChanged(nameof(FontSize));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

and then call it like:

<TextBlock Name="txtSample" Text="SomeText" 
     FontSize="{Binding Foo.FontSize}"/>

Most likely you need a DataContext = this; in your constructor for Mainwindow.xaml.cs. You also need in Mainwindow.xaml.cs that returns SampleClass.

You can only bind to public properties so the first thing to do would be to make SampleClass a property:

public ClassFoo SampleClass { get; set; }

And if you intend to set it dynamically at runtime after the constructor of the window has returned, the window should implement the INotifyPropertyChanged interface and raise change notfications for the taget property to get automatically updated.

Finally the source of the binding must be set to the window somehow. You could set the Source property of the binding explicitly or set the DataContext of the TextBlock or any of its parent element to an instance of the window.

Try this implementation of the MainWindow class together with the XAML markup you posted:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        SampleClass = new ClassFoo()
        {
            FontSize = 16
        };
    }

    private ClassFoo _sampleClass;
    public ClassFoo SampleClass
    {
        get { return _sampleClass; }
        set { _sampleClass = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM