如何绑定到动态类型的属性? [英] How to bind to a property of type dynamic?

查看:123
本文介绍了如何绑定到动态类型的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; }
}

这是非常简化的代码。为简单起见,所有课程都缺少 INotifyPropertyChanged 实现。只需看看所有的代码,就好像它正确地实现了。

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.

描述插入基本界面 IClassA IClassB 不能被考虑, ,所以我很好奇动态通过WPF中的数据绑定绑定属性。这是我试过的:

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;
    }
}

我的Xaml包含一个 TextBlock ,其DataContext设置为 CutomClass 对象。如果我将 Instance 属性的类型更改为例如IClassA并正确填写,一切正常工作。

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}" />

VS2012中的Xaml Designer / ReSharper告诉我:无法解析数据中的属性描述 对象类型的上下文

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

虽然 CodeInstance 被描述为动态的类型。但代码编译,所以我认为这可能是一个设计时问题。但是, TextBlock 仍然为空。

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.

我可能会误解原则 dynamic 在这种情况下,我不知道。因此,使用Google的搜索结果不足可能是由于不完全知道要搜索的内容而导致的。

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.

推荐答案

您可以绑定到动态属性与任何属性相同。所以你的问题必须显示在别的地方,或者 ReSharper 正在给出另一个无益的错误,(讨厌该程序)

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)

动态绑定示例:

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>

代码:

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; }
}

结果:

这篇关于如何绑定到动态类型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆