2016-06-26 31 views
0

DataContextプロパティの正しいDataTriggerバインディングは何ですか? 私はこのように結合されているデータグリッドを有する:DataContextのWPF DataGrid DataTriggerバインディングのプロパティ

XAML:の.csで

<DataGrid x:Name="dataGrid" Grid.Row="4" Grid.ColumnSpan="2" 
    ItemsSource="{Binding}" 
    CellStyle="{StaticResource RowStateStyle}" 
> 
</DataGrid> 

グリッドがデータテーブルに結合され、セルのためしたがってDataContextのプロパティとしての行を含む、DataRowViewあります。

// DataTable containing lots of rows/columns 
dataGrid.DataContext = dataTable; 

編集:ASHのソリューションを参照のうえ

は私がスタイルを編集しトリグに入れます変更されずに変更されたもの:

<Style x:Key="RowStateStyle" TargetType="{x:Type DataGridCell}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=DataContext.Row.RowState, 
       RelativeSource={RelativeSource Self}}" Value="Unchanged"> 
       <Setter Property="Foreground" Value="Green" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Path=DataContext.Row.RowState, 
       RelativeSource={RelativeSource Self}}" Value="Modified"> 
       <Setter Property="Foreground" Value="Yellow" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Path=Content.Text, 
       RelativeSource={RelativeSource Self}}" Value="test"> 
       <Setter Property="Foreground" Value="Red" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

Content.Textをトリガーすると完全に正常に動作するため、変更されません。しかし、セルを変更すると(DataRowState = Modified)、何も起こりませんし、色は緑色のままです。どんな解決策ですか?

答えて

1

I

  1. 使用DataContext.Row.RowStateパス

  2. Mode=TwoWay

  3. を使用して

を設定する際に列挙名 DataRowStateを削除しない場合はDataTriggerは、私の作品
<DataTrigger Binding="{Binding Path=DataContext.Row.RowState, 
      RelativeSource={RelativeSource Self}}" 
      Value="Unchanged"> 
    <Setter Property="Foreground" Value="Red" /> 
</DataTrigger> 

私はこの問題

WpfToolkit DataGrid: Highlight modified rows

についてRowState変更を通知デフォルトのメカニズムはありませんに関連する別のポストを発見しました。しかし、派生クラスでのdataTableを作成することが可能である:派生のDataRowクラスと

public class DataTableExt: DataTable 
{ 
    protected override DataRow NewRowFromBuilder(DataRowBuilder builder) 
    { 
     return new DataRowExt(builder); 
    } 

    protected override void OnRowChanged(DataRowChangeEventArgs e) 
    { 
     base.OnRowChanged(e); 
     // row has changed, notifying about changes 
     var r = e.Row as DataRowExt; 
     if (r!= null) 
      r.OnRowStateChanged(); 
    } 
} 

public class DataRowExt: DataRow, INotifyPropertyChanged 
{ 
    protected internal DataRowExt(DataRowBuilder builder) : base(builder) 
    { 
    } 

    internal void OnRowStateChanged() 
    { 
     OnPropertyChanged("RowState"); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

残念ながら私はまだそこにまだいませんよ。私の編集されたポストを参照してください。 – ray

+0

@ray、私の推測では、 'RowState'が変更されたとき、' DataRow'はイベントや通知を発生しません。グリッドを「再描画」(たとえば、再ソート)すると、変更が視覚化されます。 – ASh

+0

だから少しのハックか:セルが変わるたびに行を再描画するか、あるいはここからの解決策(http://stackoverflow.com/questions/5798936/inotifypropertychanged-or-inotifycollectionchanged-with-datatable)。しかし、DataGrid_CellEditEndingのようなイベントを単純にトリガするのではなく、コードを変更するだけで(コードは3行になります)、非常に面倒です。あなたは何を言っていますか? – ray

関連する問題