C# 可观察收集<;日期时间>;以及在项目更改时更新UI

C# 可观察收集<;日期时间>;以及在项目更改时更新UI,c#,wpf,xaml,C#,Wpf,Xaml,我有名为日期的ObservableCollection属性 当我执行诸如Dates[0]=DateTime.Today之类的操作时,是否有方法更新UI 或者唯一的方法是将其放入一个新类中,并在其上实现INotifyPropertyChanged?然后我必须执行Dates[0]。Date=DateTime.Today 我不想重新分配集合或清除列表,然后再次添加项目。它是这样工作的,但它是一个性能瓶颈,因为ItemsControl需要很长时间才能呈现。我要说的是,在对象上使用INotifyPrope

我有名为
日期的
ObservableCollection
属性


当我执行诸如
Dates[0]=DateTime.Today之类的操作时,是否有方法更新UI

或者唯一的方法是将其放入一个新类中,并在其上实现
INotifyPropertyChanged
?然后我必须执行
Dates[0]。Date=DateTime.Today


我不想重新分配集合或清除列表,然后再次添加项目。它是这样工作的,但它是一个性能瓶颈,因为ItemsControl需要很长时间才能呈现。

我要说的是,在对象上使用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设置一个新值时,它将通知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;
    }
}
//
///这条线的总价不含增值税
/// 
不含增值税的公共浮动价格总额
{
收到
{
返回(浮动)数学舍入(this.Qtd*(this.PricePerUnit-(this.PricePerUnit*(this.折扣/100)),2);
}
}
/// 
///返回只有2位小数的as字符串值
/// 
公共字符串GetPriceTotal不含增值税
{
收到
{
返回此.priceTotalwithout VAT.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");
    }
}
//
///生产线的数量
/// 
公共浮动Qtd
{
收到
{
把这个还给我;
}
设置
{
这是._qtd=值;
NotifyPropertyChanged(“Qtd”);
通知财产变更(“价格总计不含增值税”);
NotifyPropertyChanged(“GetPriceTotalwithout VAT”);
通知财产变更(“价格合计,含增值税”);
NotifyPropertyChanged(“GetPriceTotalWithVAT”);
}
}
当在WPF上,值下面的文本框更改为属性Qtd时,它将更新UI上其他属性的信息

<TextBox Name="TextBoxLineQtd" Grid.Column="1" Text="{Binding Qtd}" Width="70" FontSize="16" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" PreviewTextInput="ValidateNumericDecimal_PreviewTextInput"/>

此2文本框用新信息更新

<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

Dates[0]=DateTime。今天
触发CollectionChanged事件,因此应该更新UI。难道你们不能手动调用
OnPropertyChanged(nameof(Dates))吗
?@Clemens它不绑定到其他地方的
日期,仅当我替换集合或
添加
删除
项时它才起作用。@iSpain17这对更新单个项没有帮助。您可以只向视图模型添加另一个返回日期范围的属性。