wpf 如何绑定到动态类型的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14866299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:31:04  来源:igfitidea点击:

How to bind to a property of type dynamic?

c#wpfxamldata-binding

提问by Michael Schnerring

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 INotifyPropertyChangedimplementations for simplicity. Just watch all the code as if it was properly implementend.

这是非常简化的代码。INotifyPropertyChanged为简单起见,所有类都缺少实现。只需观看所有代码,就好像它已正确实现一样。

Putting Descriptioninto a base interface for IClassAand IClassBcan't be considered, yet, so I was curious about dynamicproperties bound via data binding in WPF. This is, what I have tried:

Description成基本接口IClassAIClassB可不予考虑,,所以我好奇dynamic经由数据在WPF结合结合性质。这是,我已经尝试过:

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 CutomClassobject. If I change the Instanceproperty's type to e.g. IClassA and fill it properly, everything works as expected. Not with dynamicthough.

我的 Xaml 包含一个TextBlock,它的 DataContext 设置为一个CutomClass对象。如果我将Instance属性的类型更改为例如 IClassA 并正确填充它,一切都会按预期工作。dynamic虽然不是。

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

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

VS2012 中的 Xaml Designer/ReSharper 告诉我:Cannot resolve property 'Description' in data context of type 'object'

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

虽然CodeInstance被描述为dynamic. 但是代码已编译,所以我认为这可能是设计时问题。但TextBlock空空如也。

I might misunderstand the principle dynamicin 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.

dynamic在这种情况下,我可能会误解原理,我不知道。因此,使用 Google 搜索不到结果可能是因为不知道要搜索什么。

回答by sa_ddam213

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

您可以像绑定任何属性一样绑定到动态属性。所以你的问题一定是在其他地方出现,或者ReSharper是另一个无益的错误,(讨厌那个程序)

Dynamic binding Example:

动态绑定示例:

Xaml:

Xml:

<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

在此处输入图片说明