更改内容时DataGridView样式不会更新

时间:2011-05-12 12:12:23

标签: wpf xaml datagridview styles

好的,这是我的情况: 我有一个DataGridView包含Message s,应用了以下样式。

<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属性发生更改时,也不会发生任何更改。似乎这种风格不会更新。

有人可以对此有所了解吗?谢谢!

3 个答案:

答案 0 :(得分:3)

您的Message类需要从INotifyPropertyChanged继承而IsRead属性需要在修改时引发PropertyChanged事件。这是一个例子:

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);
        }
    }
}

答案 1 :(得分:0)

您必须指定何时刷新绑定值:

<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>

每次UpdateSourceTrigger的值更改时,指定PropertyChangedIsRead都会更新值。

答案 2 :(得分:0)

我的猜测是,当IsRead属性发生更改时,您的Message类不会引发OnPropertyChanged事件。以下是如何执行此操作的简单示例:

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