Created
March 25, 2023 04:51
-
-
Save eeevans/a9c02294448ae7999f423fecf3c482e0 to your computer and use it in GitHub Desktop.
Code behind for adding buttons with FontSize binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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