如何将一个程序集中的属性绑定到另一个程序集中的另一个属性

Joh*_*ohn 0 .net c# wpf xaml

我在程序集"AssemblyX"中有一个类,其属性为"Comment".我想将AssemblyX.Comment绑定到另一个程序集中的TextBlock.Text?

我试图通过以下方式进行,但它无法正常工作.

<Window xmlns:cc="clr-namespace:SomeNamespace;assembly=AssemblyX">
<TextBlock Text={Binding cc:Comment}/>
...
Run Code Online (Sandbox Code Playgroud)

bit*_*onk 6

您通常不绑定到类的属性,绑定到类的实例的属性.因此,在您的代码隐藏中,您将创建一个实例:

SomeNamespace.SomeClass instance = new SomeClass();
instance.Comment = "bla";
this.DataContext = intstance;
Run Code Online (Sandbox Code Playgroud)

在你的xaml你绑定:

<TextBlock Text="{Binding Comment}"/>
Run Code Online (Sandbox Code Playgroud)

在这种情况下SomeClass,只要您当前项目引用该程序集,它在声明的程序集中绝对无关紧要.什么SomeClass名字也没关系.重要的是你绑定的实例有一个名为的属性Comment.

如果您的类的属性是静态的,因此您没有实例,则可以绑定到静态属性,如下所示:

<TextBlock Text="{Binding cc:SomeClass.Comment}"/> 
Run Code Online (Sandbox Code Playgroud)