内容更改时 DataGridView 样式不更新

发布于 2024-11-06 22:39:57 字数 748 浏览 1 评论 0原文

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

<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 属性时,不会发生任何变化。风格好像没有更新。

有人可以解释一下吗?谢谢!

Ok, here is my situation:
I have a DataGridView containing Messages, 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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角迷惘 2024-11-13 22:39:57

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

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);
        }
    }
}
百合的盛世恋 2024-11-13 22:39:57

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

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

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

南渊 2024-11-13 22:39:57

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

<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 指定为 PropertyChanged 将在每次 IsRead 的值更改时更新该值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文