简体   繁体   中英

ObservableCollection<DateTime> and updating the UI when the item changes

I have ObservableCollection<DateTime> property named Dates .

Is there any way to update the UI when I do something like Dates[0] = DateTime.Today .

Or the only way is to put this in a new class and implement INotifyPropertyChanged on it? Then I would have to do 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.

I would say use the INotifyPropertyChanged on a Object and use Bindings the properties of that Object.

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.

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

<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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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