Mateusz Dembski
Mateusz Dembski

Reputation: 427

RaisePropertyChanged doesn't work for ObservableCollection

I have a really weird problem with update UI using MVVM Light Toolkit. The RaisePropertyChanged doesn't work at all for my ObservableCollection.

Here is the XAML code:

<ListBox x:Name="list"  ItemsSource="{Binding ModelList}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
       <ListBox.ItemTemplate>
           <DataTemplate>
               <StackPanel Orientation="Horizontal">
                   <TextBlock Text="{Binding Name}"></TextBlock>
                   <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
               </StackPanel>
           </DataTemplate>
       </ListBox.ItemTemplate>
       <interaction:Interaction.Triggers>
           <interaction:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command="{Binding TempCommand}" CommandParameter="{Binding ElementName=list, Path=SelectedItem}"
                               PassEventArgsToCommand="True"/>
            </interaction:EventTrigger>
       </interaction:Interaction.Triggers>
   </ListBox>

And there is ViewModel code part:

private Model _selectedItem;
    public Model SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            RaisePropertyChanged("SelectedItem");
        }
    }

    private ObservableCollection<Model> _modelList;
    public ObservableCollection<Model> ModelList
    {
        get { return _modelList; }
        set
        {
            _modelList = value;
            RaisePropertyChanged("ModelList");
        }
    }

    public RelayCommand<Model> TempCommand { get; private set; }
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        modelList = new ObservableCollection<Model>()
            {
                new Model()
                    {
                        IsChecked = true,
                        Name = "Temp1"
                    },
                new Model()
                    {
                        IsChecked = false,
                        Name = "Temp2"
                    },
                new Model()
                    {
                        IsChecked = false,
                        Name = "Temp3"
                    }
            };
        ModelList = modelList;

        TempCommand = new RelayCommand<Model>(Model_SelectedItem);
    }

    private void Model_SelectedItem(Model item)
    {
        // What should I do here?
    }

When I change the ModelList - there is no reaction from ListBox UI.

Anyone can help me ?

Upvotes: 1

Views: 3088

Answers (1)

Mateusz Dembski
Mateusz Dembski

Reputation: 427

Problem solved.

When you have custom class in ObservableCollection or List it has to derived from ObservableObject and all the Properties have to fire RaisePropertyChanged event.

Upvotes: 2

Related Questions