2016-10-03 12 views
0

同じ行の2つのセルの比較値に基づいてDataTriggerを設定しようとしています。私の難しさは、細胞が同じアイテムの特性ではない(できない)ことです。グリッドはコードビハインドで生成されます。2つの列値のデータトリガー比較

public class EqualityConverter : IValueConverter 
{ 
    public object Convert(object values, Type targetType, object parameter, CultureInfo culture) 
    { 
     string currentValue = values.ToString(); 
     string compareToValue = Column[2].Item.Value.ToString(); //This clearly doesn't work, but it's the intent I'm after. 

     if (currentValue.Equals(compareToValue)) 
      return false; 
     return true; 
    } 

XAML (Binding Path=Value)は素晴らしい作品。 (ConverterParameter = Column2.Value)は私の問題点です。 どのように私はこれを取得することができます任意の提案???

<DataTrigger Binding="{Binding Path=Value, Converter={StaticResource EqualityConverter}}" Value="True"> 
    <Setter Property="Background" Value="Yellow" /> 
</DataTrigger> 

私はアイテムのプロパティにバインドすることができない理由は、セルの値は、それ自体でアイテムです。だから私は列2の「値」プロパティと比較しようとしています。すべてDataGridCell

public class GenericProperty : INotifyPropertyChanged 
{ 
    public GenericProperty(string name, object value) 
    { 
     Name = name; 
     Value = value; 
    } 

    public string Name { get; private set; } 
    public object Value { get; set; } 
+0

私は答えを見た – AnjumSKhan

+0

ちょっとAnjum、応答を与えることに私の遅れて申し訳ありません。あなたのお手伝いをしていただきありがとうございます。私の最初の試みは失敗しましたが、私はそれを見ました。しかし、私は今までこのLoadedイベントに慣れておらず、少なくとももう1つ新しいテイクを与えたいと思っています。まもなくフォローアップされます。 – ctalley5

答えて

0

は、よりシンプルで直接的な解決策を見つけるために多くの努力の後...私は、一致する値を識別するためのプレースホルダとして使用するために私のオブジェクトにプロパティを追加することになりました私のコレクションをループし、私の「列2」の項目と一致する項目のための私の新しいプロパティの値を「true」に設定する方法:最後に

 private void IdentifyMatchingItems(ObservableCollection<GenericObject> groupedCollection) 
    { 
     foreach (var collectionObject in groupedCollection) 
     { 
      int x = collectionObject.Properties.Count; 
      int propertyCount = x - 1; 
      string masterValue = collectionObject.Properties[2].Value.ToString(); 
      //   string ObjectNotNull = collectionObject.Properties[propertyNumber].Value.ToString(); 

      for (int i = 2; i <= propertyCount; i++) 
      { 
       if (collectionObject.Properties[i].Value.Equals(masterValue)) 
       { 
        collectionObject.Properties[i].Latest = "yes"; 
       } 
      } 
     } 
    } 

は、私は私のDataGridのセルがベースのスタイルdatatriggerを持っています"Match"プロパティに追加します。

1

DataContextそのDataGridRowに表示されているレコード(アイテム)です。だから、あなたがしなければならないのは、ちょうどあなたの第2のColumnに表示されるプロパティの値を比較することです。

<DataTrigger Binding="{Binding secondColumnProp}" value="True"> 
    <Setter Property="Background" Value="Yellow"/> 
</DataTrigger> 

UPDATE#1ユーザー後のあなたは、その時点でバインディングが評価されるので、DataGridCellLoadedイベントを使用する必要が

をコメントしています。私は、持っている

 public GenericProperty(string name, object value, string match) 
    { 
     Name = name; 
     Value = value; 
    ** Match = match; ** 
    } 

    public string Name { get; private set; } 
    public object Value { get; set; } 
    public string Match { get; set; } 

<DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <EventSetter Event="Loaded" Handler="DataGridCell_Loaded"/> 
     </Style> 
    </DataGrid.CellStyle> 

    void DataGridCell_Loaded(object sender, RoutedEventArgs e) 
    { 
     DataGridCell cell = sender as DataGridCell; 
     DependencyObject reference = cell; 
     while (reference.GetType() != typeof(DataGridRow)) 
      reference = VisualTreeHelper.GetParent(reference); 

     DataGridRow row = reference as DataGridRow; 

     while (reference.GetType() != typeof(DataGrid)) 
      reference = VisualTreeHelper.GetParent(reference); 

     DataGrid grid = reference as DataGrid; 
     FrameworkElement elem = grid.Columns[2].GetCellContent(row); 

     // use elem.DataContext, or traverse elem's visualtree to do something 
     // code below is just an example 
     if (elem is TextBlock) 
     { 
      System.Diagnostics.Debug.WriteLine((elem as TextBlock).Text); 
      if ((elem as TextBlock).Text == "34") 
       cell.Background = Brushes.DarkMagenta; 
     } 
    } 
+0

しかし、そこに私の問題があります。グリッドは動的に生成され、DataGridRowは単一のレコード(項目)ではありません。私はすべての動的に生成された列のためのバインディングの設定を持っている...しかし、私は列[2]にそれぞれの平等を比較する必要があります。私は明確にするために私の質問を編集します。 – ctalley5

+0

@ ctalley5更新された回答 – AnjumSKhan

+0

Anjumを参照してください。上記のソリューションに感謝します。典型的なシナリオで私はあなたの答えが最高であると信じています。多くの試みの後、私はCellstyleを使うことができませんでした。私は、DataGridを手作業でビルドし、コードビハインドからデータテンプレートを割り当てる方法を考えています。 – ctalley5