Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when binding using markup extensions: Unknown property encountered while parsing a Markup Extension

In principle, I developed a neat way to bind RadioButtons to almost anything:

/// <summary>Converts an value to 'true' if it matches the 'To' property.</summary>
/// <example>
/// <RadioButton IsChecked="{Binding VersionString, Converter={local:TrueWhenEqual To='1.0'}}"/>
/// </example>
public class TrueWhenEqual : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object To { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return object.Equals(value, To);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value) return To;
        throw new NotSupportedException();
    }
}

For example, you can use this to bind RadioButtons to a string property as follows (it is a well-known bug that you must use a unique GroupName for each RadioButton):

<RadioButton GroupName="G1" Content="Cat"
    IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='CAT'}}"/>
<RadioButton GroupName="G2" Content="Dog"
    IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='DOG'}}"/>
<RadioButton GroupName="G3" Content="Horse"
    IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='HORSE'}}"/>

Now, I would like to use public static readonly objects called Filter1 and Filter2 as the values of my RadioButtons. So I tried:

<RadioButton GroupName="F1" Content="Filter Number One"
    IsChecked="{Binding Filter, Converter={local:TrueWhenEqual To='{x:Static local:ViewModelClass.Filter1}'}}"/>
<RadioButton GroupName="F2" Content="Filter Number Two"
    IsChecked="{Binding Filter, Converter={local:TrueWhenEqual To='{x:Static local:ViewModelClass.Filter2}'}}"/>

But this gives me an error:

Unknown property 'To' for type 'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension' encountered while parsing a Markup Extension.

The error still occurs if I remove the quotes. What am I doing wrong?

like image 596
Qwertie Avatar asked Aug 02 '12 20:08

Qwertie


1 Answers

WPF doesn't handle nested markup extensions too well. To overcome this, you can use your markup extension as an element. It's a bit clumsy and harder to read, but it works:

<RadioButton GroupName="F1" Content="Filter Number One">
    <RadioButton.IsChecked>
        <Binding Path="Filter">
            <Binding.Converter>
                <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} />
            </Binding.Converter>
        </Binding>
    </RadioButton.IsChecked>
</RadioButton>

Another way would be to declare your converter and use it as a static resource:

<Window.Resources>
    <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} x:Key="myConverter" />
</Window.Resources>

<RadioButton GroupName="F1" Content="Filter Number One"
             IsChecked="{Binding Filter, Converter={StaticResource myConverter}}" />
like image 101
Adi Lester Avatar answered Oct 16 '22 15:10

Adi Lester