locked
why data trigger doesn't refresh listview? RRS feed

  • Question

  • Hi,

     

    I have a Listview with dataTriggers:

     

    <Style x:Key="MyListView" TargetType="{x:Type ListViewItem}">

    <Setter Property="Margin" Value="0,1,0,0"/>

    <Setter Property="Height" Value="21"/>

    <Style.Triggers>

    <DataTrigger Binding="{Binding Path=Won}" Value="Yes">

    <Setter Property="Background" >

    <Setter.Value>

    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

    <LinearGradientBrush.GradientStops>

    <GradientStop Color="#FF51D85A" Offset="0"/>

    <GradientStop Color="#FFFFFFFF" Offset="1"/>

    </LinearGradientBrush.GradientStops>

    </LinearGradientBrush>

    </Setter.Value>

    </Setter>

    </DataTrigger>

    ...

     

    <ListView Grid.Row="2" IsSynchronizedWithCurrentItem="True" Margin="15,20,15,20" Name="failureListViewData" ItemsSource="{Binding}" FontSize="12" ItemContainerStyle="{StaticResource MyListView}">

    <ListView.View>

    <GridView>

    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}"/>

    <GridViewColumn Header="Bidder" DisplayMemberBinding="{Binding Path=Bidder}" />

    ...

     

    I associate ListView.DataContext with object derived from ObservableCollection class.

     

    failureListViewData.DataContext = listViewData;

     

    where listViewData is an instance of:

     

    public class ListData : ObservableCollection<FailureToDisplay>

    {

    public ListData()

    {

    }

    }

     

    and FailureToDisplay class posses necessary properties (e.g. Won), that are read by DataTrigger.

     

    the data trigger is supposed to change the backlight of a row to green if a property Won of listViewData changes. The problem is, that the backlighting is green only when I add an item to listViewData which already has set Won to Yes. If the row is initialized with other value than Yes and I change this value to Yes, the collection does not refresh and backlight remains as it was.

     

    I guess the CollectionChanged event is not raised for some reason and therefore the listView does not update. I can make it working the way I want by doing something like:

     

    failureListViewData.DataContext = null;

    failureListViewData.DataContext = listViewData;

     

    after listViewData.Won property is changed. But obviously this is not the way it should be done...

     

    Can somebody please point out what is wrong in the code above or what am I supposed to add to make it working the way I intend?

    Monday, October 15, 2007 5:24 PM

Answers

  • You must implement INotifyPropertyChanged for the FailureToDisplay  Class

     

    public class FailureToDisplay: INotifyPropertyChanged

    {

      private bool _Won;

     public bool Won
            {
                get { return _Won; }
                set
                {
                    _Won= value;
                    onPropertyChanged("Won");
                }
            }

     public event PropertyChangedEventHandler PropertyChanged;

       

     

     private void onPropertyChanged(string PropName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropName));
                }
            }

    }

     

     

    Please let me know if this is not working

     

    Tuesday, October 16, 2007 7:23 AM

All replies

  • You must implement INotifyPropertyChanged for the FailureToDisplay  Class

     

    public class FailureToDisplay: INotifyPropertyChanged

    {

      private bool _Won;

     public bool Won
            {
                get { return _Won; }
                set
                {
                    _Won= value;
                    onPropertyChanged("Won");
                }
            }

     public event PropertyChangedEventHandler PropertyChanged;

       

     

     private void onPropertyChanged(string PropName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropName));
                }
            }

    }

     

     

    Please let me know if this is not working

     

    Tuesday, October 16, 2007 7:23 AM
  • I forgot to add

     

    onPropertyChanged("Won");

     

    in the Won property setter. Thanks Haridas for the help Smile

    Tuesday, October 16, 2007 3:12 PM
  • Hi,
       I want to do something similar to what you have done and I have got it working partially. I have to use myListView.Items.Refresh() to refresh the screen. In your code example where do you assign the event handler to PropertyChanged?
    Thank you.
    Aditya
    Wednesday, July 30, 2008 2:57 PM