繁体   English   中英

的ObservableCollection <DateTime> 并在项目更改时更新用户界面

[英]ObservableCollection<DateTime> and updating the UI when the item changes

我有名为Dates ObservableCollection<DateTime>属性。

当我执行类似Dates[0] = DateTime.Today类的操作时,有什么方法可以更新UI。

还是唯一的方法是将其放入新类并在其上实现INotifyPropertyChanged 然后,我将不得不做Dates[0].Date = DateTime.Today

我不想重新分配收藏集或清除列表,然后再次添加项目。 它以这种方式工作,但却是性能瓶颈,因为ItemsControl花费很长时间来呈现。

我会说在对象上使用INotifyPropertyChanged并使用Bindings该对象的属性。

例如,具有此对象:

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设置新值时,它将通知UI发生更改,并将对其进行更新。

一个更实际的示例,我将其放在程序中,该程序具有多个属性。

/// <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下方,值又更改为属性Qtd,它将更新UI上的其他信息

<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的代码有任何改进

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM