locked
WPF: How to update listview if ObservableCollection counter does not chage? RRS feed

  • Question

  • We have implement the listview with the following code:
    If ObservableCollection Programs changes, then listview update right way.
    However, if only the display value in the listitem changes, then Listview does not update
      <ListView
    
    
    
            FontSize="28"
    
    
    
            IsSynchronizedWithCurrentItem="True"
    
    
    
            x:Name="listViewAllPrograms"
    
    
    
            Width="Auto"
    
    
    
            SelectionMode="Single"
    
    
    
            AlternationCount="2"
    
    
    
            Grid.Row="1"
    
    
    
            Grid.Column="0"
    
    
    
            Margin="5,5,5,5"
    
    
    
            ItemsSource="{Binding Path=Programs, UpdateSourceTrigger=PropertyChanged}"
    
    
    
            SelectedIndex="{Binding Path=SelectProgramIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    When we detect display value inside ObservableCollection Programs has been changed, we call

    RaisePropertyChanged(

    "Programs")

     However, it does not update listview.
    How can we update/refresh listview when the Programs counter does not change and display value inside the Programs get changed?
    thx!
    Friday, September 11, 2009 10:38 PM

Answers

  • The elements in your ObservableCollection should implement INotifyPropertyChanged, and then raise the PropertyChanged event on the display property when it is changed.  For example, if you have this:

                <ListView ItemsSource="{MyItems}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DisplayValue}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    Then you want DisplayValue to update in the UI when it is changed, so when it is changed you need to raise the PropertyChanged event.

    • Proposed as answer by Zhi-Xin Ye Wednesday, September 16, 2009 10:48 AM
    • Marked as answer by Zhi-Xin Ye Tuesday, September 22, 2009 2:41 AM
    Friday, September 11, 2009 10:49 PM