question

BigH61-8942 avatar image
0 Votes"
BigH61-8942 asked Viorel-1 answered

ListView Not updating When bound ObservableCollection Changes

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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

For example, try this:

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.