2

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!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
rdumont
  • 553
  • 2
  • 6
  • 19
  • 2
    Does your `Message` class inherit from `INotifyPropertyChanged`? And does your `IsRead` property raise the PropertyChanged event? – Rachel May 12 '11 at 13:15
  • @Rachel, in fact it does not. I'll give it a shot later tonight. I didn't even know I had to inherit that interface. I would mark this as the answer if only it wasn't a comment. – rdumont May 19 '11 at 19:20

3 Answers3

3

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);
        }
    }
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
0

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.

Toni
  • 1,555
  • 4
  • 15
  • 23
Damascus
  • 6,553
  • 5
  • 39
  • 53
0

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

Matt West
  • 2,874
  • 1
  • 19
  • 13