ListView no longer changing values when adding INotifyPropertyChanged to class

Viewed 37

I have dictionary values bound to a listview that change dynamically based on which dict key is selected in a combobox. Works great. However, I'm trying to add the INotifyPropertyChanged interface to my viewmodel and when I do, even though the listview and combobox aren't using the PropertyChanged method, It's standing in the way of the listview changing. I remove INotifyPropertyChanged and it works again. I can't find any errors. The reason why I'm trying to add INotifyPropertyChanged is because I believe I need it to databind a progress bar to my application but I haven't gotten that far yet to begin that implementation. If I don't need to use INotifyPropertyChanged to implement the progress bar when databinding a view model I'm open for any guidance on how to do that.

Model -

    public class Example
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string[] GirlClothes = new string[]
        {
            "a", "b", "c"
        };

        public string[] BoyClothes = new string[]
        {
            "a", "b"
        };
    }

ViewModel -

    public class ExampleViewModel : INotifyPropertyChanged
    {        
        public RelayCommand<IList> Submit { get; }

        private Dictionary<Example, string[]> _example;
        public Dictionary<Example, string[]> Examples
        {
            get { return _example; }
            set { _example = value; }
        }

        private KeyValuePair<Example, string[]> _selectedItem;
        public KeyValuePair<Example, string[]> SelectedItem
        {
            get => _selectedItem;
            set => _selectedItem = value;
        }

        public ExampleViewModel()
        {
            Example lb = new Example();
            Examples = new Dictionary<Examples, string[]>();
            Examples.Add(new Example() { Name = "Mark" }, lb.BoyClothes);
            Examples.Add(new Example() { Name = "Sally" }, lb.GirlClothes);
            Submit = new RelayCommand<IList>(ExecuteSubmit);
        }

        private void ExecuteSubmit(IList selectedItems)
        {
            var selectedScreens = selectedItems
                .Cast<string>().ToList();
            return;
        }

        // WITHOUT THIS LISTVIEW CHANGES DYNAMICALLY WITHOUT ISSUE
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new
                    PropertyChangedEventArgs(propertyName));
            }
        }
    }

XAML -

    <Window.Resources>
        <viewmodel:ExampleViewModel x:Key="vm"></viewmodel:ExampleViewModel>
    </Window.Resources>
    <StackPanel Orientation="Horizontal" 
      DataContext="{Binding Source={StaticResource vm}}">
    
    <StackPanel Grid.Row="0" Grid.Column="1">
        <TextBlock>Name</TextBlock>
            <ComboBox
               ItemsSource="{Binding Path=Examples}"
               SelectedItem="{Binding SelectedItem}"
               DisplayMemberPath="Key.Name"
               SelectedIndex="0" />
    </StackPanel>

    <ListView ItemsSource="{Binding SelectedItem.Value}"
        x:Name="pageListView" Width=" 200">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"></StackPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsSelected, 
                        RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Button
        x:Name="Submit_Btn"
        Command="{Binding Submit}"
        CommandParameter="{Binding SelectedItems,
        ElementName=pageListView}">Submit</Button>

</StackPanel>

1 Answers

As Joe stated, I needed to invoke the RaisePropertyChanged method here


        public Dictionary<Example, string[]> Examples
        {
            get { return _example; }
            set
            {
                _example= value;
                RaisePropertyChanged ("Examples")
            }
        }

and here


        private KeyValuePair<Example, string[]> _selectedItem;
        public KeyValuePair<Example, string[]> SelectedItem
        {
            get => _selectedItem;
            set
               {
                _selectedItem = value;
                RaisePropertyChanged ("SelectedItem")
                }
        }

Related