2011-05-12 94 views
1

好的,這是我的情況: 我有一個DataGridView包含Message s,應用以下樣式。當內容發生變化時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屬性發生更改時,沒有任何更改。這似乎是風格只是不更新​​。

有人可以請說一說嗎?謝謝!

+2

你的'Message'類是否繼承自'INotifyPropertyChanged'?你的'IsRead'屬性會引發PropertyChanged事件嗎? – Rachel 2011-05-12 13:15:38

+0

@Rachel,事實上它不是。今晚晚些時候我會再試一次。我甚至不知道我必須繼承那個接口。如果只是它不是評論,我會將其標記爲答案。 – rdumont 2011-05-19 19:20:32

+0

我發佈了它作爲答案:) – Rachel 2011-05-19 19:34:54

回答

2

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

謝謝!這工作完美。 – rdumont 2011-05-20 01:07:27

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> 

指定UpdateSourceTriggerPropertyChanged將更新值每次IsRead的價值變化。

相關問題