ctalley5 ctalley5 - 1 month ago 12
C# Question

Datatrigger comparison of two column values

I am trying to set a

DataTrigger
based on the comparison values of two cells in the same row. My difficulty is the cells are not (and cannot be) properties of the same item. The grid is generated in code-behind.

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)
works great.
(ConverterParameter = Column2.Value)
is where my problem is.
Any suggestions how I can retrieve this???

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


The reason I cannot bind to the item property is the cell value is an item by itself: So I am trying to compare to the "Value" property of 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; }

Answer

After much effort to find a simpler, more direct solution... I ended up adding a property to my object to use as a placeholder to identify matching values:

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

I then have a method that loops through my Collection and sets value of my new property to "true" for items that match my "column 2" item:

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

Finally, I have a datatrigger that styles my dataGrid cell based on the "Match" property.