JohnB
JohnB

Reputation: 4359

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

Upvotes: 0

Views: 5555

Answers (3)

mm8
mm8

Reputation: 169270

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));
        }
    }
}

Upvotes: 0

AQuirky
AQuirky

Reputation: 5236

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

Upvotes: 0

miechooy
miechooy

Reputation: 3422

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}"/>

Upvotes: 2

Related Questions