0
votes

I'm playing around with WPF and databinding and I'm wondering about the following. I defined a few PropertyGroupDescriptions, but now I'm wondering how to read the PropertyName from an IValueConverter. Is this possible?

3
What's your usage? What are you passing to the converter?Julien Lebosquain
It's in use in a DataGrid for grouping. I want to adjust headers according to its propertyOxymoron
which doesnt work as i expected, too bad :(Oxymoron

3 Answers

1
votes

You can't. The IValueConverter interface does not have any methods which take the property which is being converted.

It might have been nice to have the PropertyInfo or PropertyDescriptor instance passed to the Convert and ConvertBack methods, but the designers didn't find it necessary.

The only way around this is to set the IValueConverter implementation in code, then in the construction of the implementation, you can pass the property that the IValueConverter interface implementation is being attached to.

0
votes

There's always the cheap way of passing the name via the binding's ConverterParameter, though that's not exactly a "clean" way of doing it.

0
votes

Maybe I should elaborate a bit more. I have the following groups in my xaml:

<CollectionViewSource x:Key="cvsTasks" Source="{StaticResource tasks}" Filter="CollectionViewSource_Filter" >            
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="ProjectName" />
                <PropertyGroupDescription PropertyName="TaskName" />
                <PropertyGroupDescription PropertyName="Complete" />
            </CollectionViewSource.GroupDescriptions>

and

<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>

Now I want a converter to be valled on Name, which is fine, but I only want it to actually work on the property Complete. What would be the best way of doing that?

I end up with fugly code like:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                if (value.GetType().Equals(typeof(Boolean)))
                {
                    return (bool)value ? "Complete" : "Active";
                }
                if (value.GetType().Equals(typeof(String)))
                {
                    return value as string;
                }
            }
            return null;
        }

Seems, wrong.