[英]ObservableCollection<DateTime> and updating the UI when the item changes
I have ObservableCollection<DateTime>
property named Dates
. 我有名为
Dates
ObservableCollection<DateTime>
属性。
Is there any way to update the UI when I do something like Dates[0] = DateTime.Today
. 当我执行类似
Dates[0] = DateTime.Today
类的操作时,有什么方法可以更新UI。
Or the only way is to put this in a new class and implement INotifyPropertyChanged
on it? 还是唯一的方法是将其放入新类并在其上实现
INotifyPropertyChanged
? Then I would have to do Dates[0].Date = DateTime.Today
然后,我将不得不做
Dates[0].Date = DateTime.Today
I don't want to re-assign the collection or clear the list and then add items again. 我不想重新分配收藏集或清除列表,然后再次添加项目。 It works that way but is a performance bottleneck because ItemsControl takes long to render.
它以这种方式工作,但却是性能瓶颈,因为ItemsControl花费很长时间来呈现。
I would say use the INotifyPropertyChanged on a Object and use Bindings the properties of that Object. 我会说在对象上使用INotifyPropertyChanged并使用Bindings该对象的属性。
For example, having this object: 例如,具有此对象:
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));
}
}
When you set a new value for the DateTime it will notify the UI there is a change and it will update it. 当您为DateTime设置新值时,它将通知UI发生更改,并将对其进行更新。
A more practical example, I have this on a program, an object with several properties. 一个更实际的示例,我将其放在程序中,该程序具有多个属性。
/// <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;
}
}
And we have the property with the set here: 我们在此处具有设置的属性:
/// <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");
}
}
When on the WPF the TextBox bellow the value changes AKA the property Qtd it will update the information on the UI for the other ones 在WPF上的TextBox下方,值又更改为属性Qtd,它将更新UI上的其他信息
<TextBox Name="TextBoxLineQtd" Grid.Column="1" Text="{Binding Qtd}" Width="70" FontSize="16" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" PreviewTextInput="ValidateNumericDecimal_PreviewTextInput"/>
this 2 TextBox are updated with the new information 这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"/>
Hope that this helped out, if you guys see any improvement on the code that I have put here to tell :D 希望这对您有所帮助,如果你们看到我在这里告诉:D的代码有任何改进
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.