How to bind the property change in my window when the change happens in another class?

1 day ago 1
ARTICLE AD BOX

You are mixing two concepts: on one hand you want to use data-binding, on the other you created a lot of logic in code behind (i.e. *.xaml.cs files) in order to handle value changes. The whole point of data binding is to leave the *.xaml.cs files untouched (i.e. no added code there) and use data binding in *.xaml files.

For the example I modified BankAccount.cs slightly.

using System.ComponentModel; using System.Runtime.CompilerServices; namespace MultiWindowBind; public class BankAccount : INotifyPropertyChanged { private decimal _balance; public decimal Balance { get => _balance; set // <-----------changed to be public for this example { if (_balance != value) { _balance = value; NotifyPropertyChanged(); } } } public event PropertyChangedEventHandler? PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }

I created a Dashboard.xaml with a TextBox to edit the Balance property:

<Window x:Class="MultiWindowBind.Dashboard" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MultiWindowBind" d:DataContext="{d:DesignInstance Type=local:BankAccount}" mc:Ignorable="d" Title="Dashboard" Height="200" Width="300"> <TextBox Text="{Binding Balance, UpdateSourceTrigger=PropertyChanged}"/> </Window>

As stated before, I did not add any logic to Dashboard.xaml.cs:

using System.Windows; namespace MultiWindowBind { /// <summary> /// Interaction logic for Dashboard.xaml /// </summary> public partial class Dashboard : Window { public Dashboard() { InitializeComponent(); } } }

I added a second window YetAnotherDashboard.xaml which displays the value of the Balance property through a TextBlock and a Label:

<Window x:Class="MultiWindowBind.YetAnotherDashboard" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MultiWindowBind" d:DataContext="{d:DesignInstance Type=local:BankAccount}" mc:Ignorable="d" Title="YetAnotherDashboard" Height="200" Width="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Content="Balance"/> <TextBlock Text="{Binding Balance}" Grid.Column="1"/> </Grid> </Window>

Again, no logic added to YetAnotherDashboard.xaml.cs:

using System.Windows; namespace MultiWindowBind { /// <summary> /// Interaction logic for YetAnotherDashboard.xaml /// </summary> public partial class YetAnotherDashboard : Window { public YetAnotherDashboard() { InitializeComponent(); } } }

To tie them together, you need to create one single BankAccount object that's set to both windows' DataContext and thus shared among them. During initialization of the app, I do that like so:

var account = new BankAccount(); var dashboard = new Dashboard { DataContext = account }; dashboard.Show(); var yetAnotherDashboard = new YetAnotherDashboard { DataContext = account }; yetAnotherDashboard.Show();

That's all you need. When running the app you can see that the value 123645 displayed in YetAnotherDashboard is bound to be the same that's entered in Dashboard:

3 window application

Read Entire Article