Я бы сказал, использовать INotifyPropertyChanged для объекта и использовать привязки свойства этого объекта.
Например, имея этот объект:
public class Dates : INotifyPropertyChanged
{
private DateTime _myDate;
public DateTime MyDate
{
get
{
return _myDate;
}
set
{
_myDate = value;
// With this NotifyPropertyChanged it will raise the event that it has changed and update the information where there is a binding for this property
NotifyPropertyChanged("MyDate");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Когда вы устанавливаете новое значение дляDateTime уведомит пользовательский интерфейс об изменении и обновит его.
Более практический пример - это программа, объект с несколькими свойствами.
/// <summary>
/// Total price for this line without VAT
/// </summary>
public float PriceTotalWithoutVAT
{
get
{
return (float)Math.Round(this.Qtd * (this.PricePerUnit - (this.PricePerUnit * (this.Discount / 100))), 2);
}
}
/// <summary>
/// Returns the value of <seealso cref="PriceTotalWithoutVAT"/> as string with only 2 decimal places
/// </summary>
public string GetPriceTotalWithoutVat
{
get
{
return this.PriceTotalWithoutVAT.ToString("0.00") + RegionInfo.CurrentRegion.CurrencySymbol;
}
}
И у нас есть свойство с установленным здесь:
/// <summary>
/// Quantity for the line
/// </summary>
public float Qtd
{
get
{
return this._qtd;
}
set
{
this._qtd = value;
NotifyPropertyChanged("Qtd");
NotifyPropertyChanged("PriceTotalWithoutVAT");
NotifyPropertyChanged("GetPriceTotalWithoutVat");
NotifyPropertyChanged("PriceTotalWithVAT");
NotifyPropertyChanged("GetPriceTotalWithVAT");
}
}
Когда в WPF TextBox ниже значение меняет AKA свойство Qtd, оно обновит информацию об интерфейсе для других
<TextBox Name="TextBoxLineQtd" Grid.Column="1" Text="{Binding Qtd}" Width="70" FontSize="16" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" PreviewTextInput="ValidateNumericDecimal_PreviewTextInput"/>
это 2 TextBox обновлено новой информацией
<TextBox Name="TextBoxLineTotalWihtoutVat" Grid.Column="1" Text="{Binding GetPriceTotalWithoutVat, Mode=OneWay}" Width="100" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" IsReadOnly="True" IsTabStop="False"/>
<TextBox Name="TextBoxLineTotalWihtVat" Grid.Column="3" Text="{Binding GetPriceTotalWithVAT, Mode=OneWay}" Width="100" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" IsReadOnly="True" IsTabStop="False"/>
Надеюсь, что это помогло, если вы, ребята, увидите какие-нибудь улучшения в коде, который я поместил здесь, чтобы сказать: D