I have create a class for the Data Context
public class Amt
{
private double _Amount = 0;
public double Amount
{
get { return _Amount; }
set { _Amount = value; }
}
private double _RedeemAmount = 0;
public double RedeemAmount
{
get { return _RedeemAmount; }
set
{
_RedeemAmount = value;
}
}
private double _BalAmount = 0;
public double BalAmount
{
get { return Amount - RedeemAmount; }
}
}
And I have bind it to TextBox at XAML
<TextBox Name="txtAmount" Text="{Binding Amount}" FontSize="16" Margin="0,0,0,2"/>
<TextBox Name="txtRedeemAmount" Text="{Binding RedeemAmount,Mode=TwoWay}" FontSize="16" Margin="0,0,0,2" TextChanged="TxtRedeemAmount_TextChanged"/>
<TextBox Name="txtBalAmount" Text="{Binding BalAmount}" FontSize="16" Margin="0,0,0,2"/>
Then, what I want is when I change the Redeem Amount in TextChanged event, I expect the Balance amount should change too, I have do some research and try some code on it, but it just not working. Did I do this in wrong way? Any other ways to do this better?
private void TxtRedeemAmount_TextChanged(object sender, EventArgs e)
{
double amt = 0;
double.TryParse(txtRedeemAmount.Text, out amt);
Amt.RedeemAmount = amt;
}
Thanks for the help anyway!!!
To accomplish what you want, you need to implement the INotifyPropertyChanged interface. Whenever a property of the class is changed you should fire the PropertyChanged event.
using System.ComponentModel;
public class Amt : INotifyPropertyChanged
{
private double _Amount = 0;
public double Amount
{
get { return _Amount; }
set
{
_Amount = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Amount)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BalAmount)));
}
}
private double _RedeemAmount = 0;
public double RedeemAmount
{
get { return _RedeemAmount; }
set
{
_RedeemAmount = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RedeemAmount)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BalAmount)));
}
}
private double _BalAmount = 0;
public double BalAmount
{
get { return Amount - RedeemAmount; }
}
public event PropertyChangedEventHandler PropertyChanged;
}