ListView Not updating When bound ObservableCollection Changes

BigH61 581 Reputation points
2022-08-05T17:05:56.573+00:00

I have a simple test application were the selection in a ComboBox changes the content of an ObservableCollection which in turn is bound to a ListView. My problem is the ListView does not update on ComboBox Selection Changed. This is only intended as a simple test to identify a problem in a bigger project.

xaml

 <ComboBox Grid.Column="0" Grid.Row="1" IsEditable="True" SelectionChanged="ComboBox_SelectionChanged">  
                <ComboBoxItem Content="Me"/>  
                <ComboBoxItem Content="Them"/>  
            </ComboBox>  
            <ScrollViewer Grid.Column="0" Grid.Row="6" Margin="0,0,0,-168">  
                <ListView  ItemsSource="{Binding Person, Mode=OneWay}" Height="250">  
                    <ListView.View>  
                        <GridView ColumnHeaderContainerStyle="{StaticResource GridViewHeaderStyle}">  
                            <GridViewColumn Header=" " >  
                                <GridViewColumn.CellTemplate>  
                                    <DataTemplate>  
                                        <CheckBox FocusVisualStyle="{x:Null}" IsChecked="{Binding Include}"/>  
                                    </DataTemplate>  
                                </GridViewColumn.CellTemplate>  
                            </GridViewColumn>  
                            <GridViewColumn Header="Name " DisplayMemberBinding="{Binding Path=.}"/>  
                        </GridView>  
                    </ListView.View>  
                </ListView>  
            </ScrollViewer>  

 

xaml Style

<Style x:Key="GridViewHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">  
        <Setter Property="Visibility" Value="Collapsed" />  
    </Style>  

CodeBehind
public ObservableCollection<String> Person { get; set; }

        public MainWindow()  
        {  
            InitializeComponent();  
            DataContext = this;  
            Person = new ObservableCollection<string>()  
            {  
                "Margaret","John","Stephen","Yvonne","Angela",  
            };  
        }  
  
  
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
            Person.Clear();  
            Person = new ObservableCollection<string>()  
            {  
                "Cath","Maggie","Caitlin",  
            };  
        }  

Any assistance would be appreciated.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,679 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2022-08-05T19:20:05.983+00:00

    For example, try this:

    private void ComboBox_SelectionChanged( object sender, SelectionChangedEventArgs e )  
    {  
        Person.Clear( );  
      
        Person.Add( "Cath" );  
        Person.Add( "Maggie" );  
        Person.Add( "Caitlin" );  
    }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful