Michael Schnerring
Michael Schnerring

Reputation: 3661

How to bind to a property of type dynamic?

public interface IClassA
{
    string Description { get; set; }
    // more IClassA specific definitions
}

public interface IClassB
{
    string Description { get; set; }
    // more IClassB specific definitions
}

public class ClassA : IClassA
{
    public string Description { get; set; }
}

public class ClassB : IClassB
{
    public string Description { get; set; }
}

This is the very simplified code. All classes lack of INotifyPropertyChanged implementations for simplicity. Just watch all the code as if it was properly implementend.

Putting Description into a base interface for IClassA and IClassB can't be considered, yet, so I was curious about dynamic properties bound via data binding in WPF. This is, what I have tried:

public class CustomClass
{
    public dynamic Instance { get; set; }

    // passed instance is a ClassA or ClassB object
    public CustomClass(dynamic instance)
    {
        Instance = instance;
    }
}

My Xaml contains a TextBlock, which has its DataContext set to a CutomClass object. If I change the Instance property's type to e.g. IClassA and fill it properly, everything works as expected. Not with dynamic though.

<TextBlock Text="{Binding Instance.Description}" />

The Xaml Designer/ReSharper in VS2012 tells me:Cannot resolve property 'Description' in data context of type 'object'.

Though CodeInstance is described as type of dynamic. But the code compiled, so I thought that just might be a design-time issue. But the TextBlock remains empty.

I might misunderstand the principle dynamic in this case, I don't know. So the lack of search results by using Google might be caused by not exactly knowing what to search for.

Upvotes: 1

Views: 7250

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43636

You can bind to a dynamic property the same way as any property. So your problem must be manifesting somewhere else, or ReSharper is giving yet another unhelpful error, (hate that program)

Dynamic binding Example:

Xaml:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="104" Width="223" Name="UI">
    <StackPanel DataContext="{Binding ElementName=UI}">
        <TextBlock Text="{Binding MyProperty}" />
        <TextBlock Text="{Binding MyObject.MyProperty}" />
    </StackPanel>
</Window>

Code:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        MyProperty = "Hello";
        MyObject = new ObjectTest { MyProperty = "Hello" };
    }

    private dynamic myVar;
    public dynamic MyProperty
    {
        get { return myVar; }
        set { myVar = value; NotifyPropertyChanged("MyProperty"); }
    }

    private dynamic myObject;
    public dynamic MyObject
    {
        get { return myObject; }
        set { myObject = value; NotifyPropertyChanged("MyObject"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}

public class ObjectTest
{
    public string MyProperty { get; set; }
}

Result:

enter image description here

Upvotes: 3

Related Questions