简体   繁体   English

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

[英]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.

相关问题 在ObservableCollection中更改模型属性时更新UI? - Updating UI when a model property changes in an ObservableCollection? 将新项目添加到 ObservableCollection 时,UI 仅更新一次 - UI only updating once when adding new item to ObservableCollection 当 Item 改变时通知 ObservableCollection - Notify ObservableCollection when Item changes 从UI更改ObservableCollection而不更新Bound Collection - Changes to ObservableCollection from UI not updating the Bound Collection Winui3 桌面; ObservableCollection,在属性更改时更新 UI? 从不同线程更新 - Winui3 Desktop; ObservableCollection, updating UI when property changes? Updating from different thread 基础ObservableCollection更改时,ComboBox项未更新 - ComboBox items not updating when underlying ObservableCollection changes 绑定的ObservableCollection更改时,ListView不更新 - ListView not updating when the bound ObservableCollection changes 当ObservableCollection的值更改时,UI不更新 - UI is not updated when value of ObservableCollection Changes 更改ObservableCollection的内容时难以更新UI? - Difficulty updating UI when contents of ObservableCollection is changed? ObservableCollection 没有注意到其中的 Item 发生变化(即使使用 INotifyPropertyChanged) - ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM