问题描述
好的,这是我的情况:
我有一个 DataGridView
包含消息
s,应用以下样式。
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>
我的目的是使未读消息粗体,同时阅读消息保持正常的字体重量。即使加载集合时样式正确应用,当项目的 IsRead
属性更改时,也不会更改。似乎这种风格没有更新。
My intention is to make unread messages bold, while read 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!
推荐答案
您的消息
类需要继承自 INotifyPropertyChanged
,修改后, IsRead
属性需要提升 PropertyChanged
事件。以下是一个例子:
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);
}
}
}
这篇关于内容更改时,DataGridView样式不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!