Datatrigger比较两个列值

时间:2016-10-03 03:44:43

标签: c# wpf datatrigger ivalueconverter

我正在尝试根据同一行中两个单元格的比较值设置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>

我无法绑定到item属性的原因是单元格值本身是一个项目:所以我试图比较&#34; Value&#34; Column2的财产。

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

2 个答案:

答案 0 :(得分:1)

每个DataContext

DataGridCellDataGridRow中显示的记录(项目)。因此,您只需要比较第二个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;
        }
    }

答案 1 :(得分: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; }

然后我有一个方法循环我的Collection并将我的新属性的值设置为“true”,用于匹配我的“第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单元格。