ObservableCollection not updating View

前端 未结 3 1361
误落风尘
误落风尘 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<EMSMenuItem> Level2MenuItems { get; set; }
    

    you need

    private ObservableCollection<EMSMenuItem> _level2MenuItems;
    public ObservableCollection<EMSMenuItem> 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

    0 讨论(0)
  • 2020-12-01 21:46

    You need to make your poco class within the ObservableCollection implement INotifyPropertyChanged.

    Example:

    <viewModels:LocationsViewModel x:Key="viewModel" />
    .
    .
    .    
    <ListView
        DataContext="{StaticResource viewModel}"
        ItemsSource="{Binding Locations}"
        IsItemClickEnabled="True"
        ItemClick="GroupSection_ItemClick"
        ContinuumNavigationTransitionInfo.ExitElementContainer="True">
    
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Margin="0,0,10,0" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                    <TextBlock Text="{Binding Latitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{ThemeResource ListViewItemTextBlockStyle}" Margin="0,0,5,0"/>
                    <TextBlock Text="{Binding Longitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{ThemeResource ListViewItemTextBlockStyle}" Margin="5,0,0,0" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    
    public class LocationViewModel : BaseViewModel
    {
        ObservableCollection<Location> _locations = new ObservableCollection<Location>();
        public ObservableCollection<Location> Locations
        {
            get
            {
                return _locations;
            }
            set
            {
                if (_locations != value)
                {
                    _locations = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    }
    
    public class Location : BaseViewModel
    {
        int _locationId = 0;
        public int LocationId
        {
            get
            {
                return _locationId;
            }
            set
            {
                if (_locationId != value)
                {
                    _locationId = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        string _name = null;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        float _latitude = 0;
        public float Latitude 
        { 
            get
            {
                return _latitude;
            }
            set
            {
                if (_latitude != value)
                {
                    _latitude = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        float _longitude = 0;
        public float Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                if (_longitude != value)
                {
                    _longitude = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    }
    
    public class BaseViewModel : INotifyPropertyChanged
    {
        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    
        protected void OnNotifyPropertyChanged([CallerMemberName] string memberName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(memberName));
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 21:50

    Your Subcategories property should be read-only.

    0 讨论(0)
提交回复
热议问题