0
votes

Is it possible to use an attached property as parameter of a Multibinding? How?

I need to use the value of an attached property to change the text I bind to a TextBlock.

My code is similar to this:

TextBlock Binding

 <TextBlock x:Name="myTxt" 
        wpfApplication2:TextBlockAttachedProperties.MyProperty="true">
<TextBlock.Text>
    <MultiBinding Converter="{StaticResource CustomConverter}"  Mode="TwoWay" NotifyOnValidationError="true">
        <Binding Path="Test"/>
        <Binding ElementName="myTxt" Path="MyProperty" Mode="OneWay"/>
    </MultiBinding>
</TextBlock.Text>

My converter:

Converter Code

 public class CustomConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double? value = null;

        value = values[0] as double?;
        DependencyProperty myProperty= null;

        if (values.Count() > 1 && values[1] != DependencyProperty.UnsetValue) 
            //Do something

        if (myProperty!= null )
        {
            //here do something with the value using the attached property
            var convertedValue = value; 

            return convertedValue;
        }

        return value;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        object[] values = { value };
        return values;
    }
}

However since I don't know how to pass the attached property as a parameter of the Multibinding the converter always gets DependencyProperty.UnsetValue.

1

1 Answers

1
votes

The MultiBinding should look like shown below, where the name of the attached property is written in parentheses (see PropertyPath XAML Syntax):

<MultiBinding Converter="{StaticResource CustomConverter}">
    <Binding Path="Test"/>
    <Binding Path="(local:TextBlockAttachedProperties.MyProperty)"
             RelativeSource="{RelativeSource Self}"/>
</MultiBinding>

Moreover, the type of the property in the value converter is not DependencyProperty, but just bool (i.e. the type of TextBlockAttachedProperties.MyProperty):

bool myProperty = (bool)values[1];