2016-10-03 72 views
0

我想根据同一行中两个单元格的比较值设置一个DataTrigger。我的困难是单元格不是(也不能是)同一项目的属性。网格在代码隐藏中生成。两列值的数据触发比较

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的“值”属性。

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

嘿安茹姆,抱歉我迟迟没有给出答复。首先非常感谢你的帮助。虽然我第一次尝试失败,但我已经看到了。但是 - 我对这个Loaded事件至今还不熟悉,并且希望至少再给它一次新的采访。很快会跟进。 – ctalley5

回答

0

多大力气就找到了更简单,更直接的解决方案之后......我最终将属性添加到我的对象作为一个占位符用来识别匹配值:

 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; } 

然后,我有通过我的收藏循环,并设置我的新属性的值设置为“真”为配合我的“列2”项目的项目的方法:

 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在“匹配”属性上。

1

DataGridCellDataContext是记录(项目)显示在该DataGridRow。因此,您只需比较第二个Column中显示的财产的价值。

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

更新#1用户后评论

你必须使用Loaded事件DataGridCell,因为这时的绑定将进行评估。

<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