DataGridView style does not update when content changes

Ok, here is my situation: I have a DataGridView containing Message s, to which the following style applies.

 <Style x:Key="ChangeSetRowStyle" TargetType="{x:Type DataGridRow}"> <Setter Property="FontWeight" Value="Normal" /> <Style.Triggers> <DataTrigger Binding="{Binding IsRead}" Value="False"> <Setter Property="FontWeight" Value="Bold" /> </DataTrigger> <DataTrigger Binding="{Binding IsRead}" Value="True"> <Setter Property="FontWeight" Value="Normal" /> </DataTrigger> </Style.Triggers> </Style> 

My intention is to make unread messages bold, and read messages remain with the normal font weight. Despite the fact that the style is applied correctly when loading the collection, nothing changes when the item IsRead is changed. It seems that the style is simply not being updated.

Can someone shed some light on this? Thanks!

+1
source share
3 answers

Your Message class should inherit from INotifyPropertyChanged , and the IsRead property should raise the PropertyChanged event when it changes. Here is an example:

 public class Message: INotifyPropertyChanged { private bool _isRead; public bool IsRead { get { return _isRead; } set { _isRead = value; RaisePropertyChanged("IsRead"); } } #region INotifyPropertyChanged Members /// <summary> /// Raised when a property on this object has a new value. /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion /// <summary> /// Raises this object PropertyChanged event. /// </summary> /// <param name="propertyName">The property that has a new value.</param> public virtual void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(this, e); } } } 
+2
source

You must specify when to update the binding value:

 <Style.Triggers> <DataTrigger Binding="{Binding IsRead, UpdateSourceTrigger=PropertyChanged}" Value="False"> <Setter Property="FontWeight" Value="Bold" /> </DataTrigger> <DataTrigger Binding="{Binding IsRead, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="FontWeight" Value="Normal" /> </DataTrigger> </Style.Triggers> 

Pointing UpdateSourceTrigger to a PropertyChanged will update the value each time the IsRead value IsRead .

0
source

I assume that your Message class does not throw an OnPropertyChanged event when the IsRead property changes. Here is a simple example of how you do this:

http://msdn.microsoft.com/en-us/library/ms743695.aspx

0
source

All Articles