Manish Basantani
Manish Basantani

Reputation: 17509

How to resolve/control property name collision in XAML binding?

Interview Question

Phrased as:

If you have a property name collision, how would you specify the exact property to bind to in a Binding path expression (in XAML)?

I never faced this (property name collision) problem in any binding so far. With some reading I realized that this is possible in case I am binding to a overridden property because then I have two instances of this property (virtual in base, and overriden in derived) as far as resolution using Reflection is concerned. Which is what used by XAML.

Thanks for your interest.

Upvotes: 3

Views: 379

Answers (2)

Mark Synowiec
Mark Synowiec

Reputation: 5445

The best way I can think would be to use a ValueConverter. I don't think this really answers the question though since they're asking in a binding path expression, which I haven't seen to be possible. I'm not particularly fond of doing it this way because it feels like a hack, but it works at least for one way binding. Here's an example of how you might do it:

XAML:

<StackPanel Name="stack">
    <StackPanel.Resources>
        <loc:OverriddenMyPropertyConverter x:Key="BaseMyProperty"/>
    </StackPanel.Resources>
    <TextBox Text="{Binding Path=MyProperty}"/>
    <TextBox Text="{Binding Mode=OneWay, Converter={StaticResource BaseMyProperty}}"/>
</StackPanel>

The DataContext of the StackPanel is an instance of MyClass. The first TextBox is bound to the MyClass.MyProperty property, and the second TextBox will be bound to the MyBaseClass.MyProperty property. Two way binding would be a bit more complex since the object actually being bound to the second TextBox is the MyClass object and not the MyProperty object.

Code:

class MyClass : MyBaseClass
{
    string myProperty = "overridden";
    public new string MyProperty
    {
        get { return myProperty; }
        set { myProperty = value; }
    }
}

class MyBaseClass
{
    string baseProperty = "base";
    public string MyProperty
    {
        get { return baseProperty; }
        set { baseProperty = value; }
    }
}

class OverriddenMyPropertyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value as MyBaseClass).MyProperty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 0

user572559
user572559

Reputation:

Sounds like a complete nonsense to me. Unless they wanted to talk about bindings, using 'disjointed' sources like PriorityBinding and MultiBinding.

Frankly speaking I don't think overwritten properties can be involved into the matter as this is so much out of scope, you could equaly point out explicit interface implementations and many other things, which are clearly outside of WPF domain.

Upvotes: 2

Related Questions