Community Toolkit WPF MVVM [Observable Property] not updating UI - Stack Overflow

I'm new to the toolkit and have made a couple of simple applications to test various scenarios. Th

I'm new to the toolkit and have made a couple of simple applications to test various scenarios. This time I've hooked up Entity Framework to start building a bigger application.

Unfortunately I've fallen at the first hurdle. My [Observable Property] is not being observed?

    [ObservableProperty]
    private List<TblAddress>? _addresses;

    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
    }

A breakpoint inside the override confirms that PropertyChanged has been fired and the Addresses List has been populated, but the View is not updating.

If (while running) I change the DataContext, then change back it shows the data as expected. Is there something I'm missing?

I'm new to the toolkit and have made a couple of simple applications to test various scenarios. This time I've hooked up Entity Framework to start building a bigger application.

Unfortunately I've fallen at the first hurdle. My [Observable Property] is not being observed?

    [ObservableProperty]
    private List<TblAddress>? _addresses;

    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
    }

A breakpoint inside the override confirms that PropertyChanged has been fired and the Addresses List has been populated, but the View is not updating.

If (while running) I change the DataContext, then change back it shows the data as expected. Is there something I'm missing?

Share Improve this question asked Mar 26 at 22:53 Richard HarrisonRichard Harrison 3856 silver badges19 bronze badges 2
  • In case you expect the UI to update when you add elements to the List, that won't happen. Use an ObservableCollection then. – Clemens Commented Mar 27 at 6:22
  • If you bind to a null collection then even if you set the property later, you may see no change – Andy Commented Mar 27 at 19:59
Add a comment  | 

1 Answer 1

Reset to default 0

It may be time to move away from the [ObservableProperty] and use the field contextual keyword. It is part of C#13 and can be used in preview. Edit your project file and set

<LangVersion>preview</LangVersion>

Below are some properties that I use that require a property change notification

public string? Image { get => field; set => Notify(ref field, value); }
public string? Description { get; set => Notify(ref field, value); }
public decimal? Amount { get; set => Notify(ref field, value); }

public event PropertyChangedEventHandler? PropertyChanged;
public void Notify<V>(ref V to, V from, [CallerMemberName] string property = "")
{
    if (property == null)
    {
        return;
    }

    if (to == null && from == null)
    {
        return;
    }

    if (to != null && to.Equals(from))
    {
        return;
    }

    to = from;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}

I reference the field contextual keyword in the get accessor of the Image property although a get accessor that does not specify a backing field will automatically return field.

https://learn.microsoft/en-us/dotnet/csharp/language-reference/keywords/field

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744122883a4559482.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信