Skip to content

Instantly share code, notes, and snippets.

@eeevans
Created March 25, 2023 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eeevans/a9c02294448ae7999f423fecf3c482e0 to your computer and use it in GitHub Desktop.
Save eeevans/a9c02294448ae7999f423fecf3c482e0 to your computer and use it in GitHub Desktop.
Code behind for adding buttons with FontSize binding
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace Wpf.Binding;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private AppSession appSession;
public MainWindow()
{
InitializeComponent();
appSession = new AppSession();
this.DataContext = appSession;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
var btn = new Button();
btn.Content = "Added Button";
ButtonList.Items.Add(btn);
}
private void ToggleSizeButton_Click(object sender, RoutedEventArgs e)
{
appSession.FontSizeLabel = appSession.FontSizeLabel < 20 ? 32 : 16;
}
}
public class AppSession : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged = (o,e) => {};
double _fontSizeLabel = 16;
public double FontSizeLabel { get { return _fontSizeLabel; } set { _fontSizeLabel = value; OnPropertyChanged("FontSizeLabel"); } }
private void OnPropertyChanged(string name)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment