DataGridView style not updating when content is changed
Ok, here is my situation:
I have a DataGridView
containing Message
s, to which the following style is applied.
<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, while r开发者_JAVA技巧ead messages stay with normal font weight. Even though the style is applied correctly when the collection is loaded, nothing changes when an item's IsRead
property is changed. It seems like the style just does't update.
Can someone please shed some light on this? Thanks!
Your Message
class needs to inherit from INotifyPropertyChanged
and the IsRead
property needs to raise the PropertyChanged
event when modified. Here's 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's 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);
}
}
}
My guess would be that your Message class is not raising an OnPropertyChanged event when the IsRead property is changed. Here is a simple example of how you do this:
http://msdn.microsoft.com/en-us/library/ms743695.aspx
You have to specify when you want the binding value to be refreshed:
<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>
Specifying UpdateSourceTrigger
to PropertyChanged
will update the value each time IsRead
's value changes.
精彩评论