ObservableCollection not updating View

前端 未结 3 1373
误落风尘
误落风尘 2020-12-01 21:15

I am just starting with MVVM and have hit a hurdle that I hope someone can help me with. I am trying to create a simple View with 2 listboxes. A selection from the first lis

3条回答
  •  没有蜡笔的小新
    2020-12-01 21:45

    You need to raise the change notification on the Level2MenuItems property.

    Instead of having

    public ObservableCollection Level2MenuItems { get; set; }
    

    you need

    private ObservableCollection _level2MenuItems;
    public ObservableCollection Level2MenuItems
    {
        get { return _level2MenuItems; }
        set 
         {
            _level2MenuItems = value; 
            RaisePropertyChanged("Level2MenuItems");
         }
     }
    

    The reason the former works in the constructor is that the Binding has not taken place yet. However since you are changing the reference via a command execute which happens after the binding you need to tell view that it changed

提交回复
热议问题