XAML定义如何变成对象实例?

时间:2021-05-23 16:53:33

XAML allows you to specify an attribute value using a string that contains curly braces. Here is an example that creates a Binding instance and assigns it to the Text property of the TextBox element.

XAML允许您使用包含花括号的字符串指定属性值。下面是一个创建Binding实例并将其分配给TextBox元素的Text属性的示例。

<TextBox Text="{Binding ElementName=Foo, Path=Bar}"/>

I want to extend XAML so that the developer could enter this as valid...

我想扩展XAML,以便开发人员可以将其输入为有效...

<TextBox Text="{MyCustomObject Field1=Foo, Field2=Bar}"/>

This would create an instance of my class and set the Field1/Field2 properties as appropriate. Is this possible? If so how do you do it?

这将创建我的类的实例并根据需要设置Field1 / Field2属性。这可能吗?如果是这样你怎么做?

If this is possible I have a followup question. Can I take a string "{Binding ElementName=Foo, Path=Bar}" and ask the framework to process it and return the Binding instance it specified? This must be done somewhere already to make the above XAML work and so there must be a way to ask for the same thing to be processed.

如果这是可能的,我有一个后续问题。我可以使用字符串“{Binding ElementName = Foo,Path = Bar}”并要求框架处理它并返回它指定的Binding实例吗?这必须在某个地方完成,以使上述XAML工作,因此必须有一种方法来要求处理相同的事情。

2 个解决方案

#1


2  

take a look at markupextensions http://blogs.msdn.com/wpfsdk/archive/2007/03/22/blogpost-text-creatingasimplecustommarkupextension.aspx

看看markupextensions http://blogs.msdn.com/wpfsdk/archive/2007/03/22/blogpost-text-creatingasimplecustommarkupextension.aspx

#2


1  

The Binding class is a Markup Extension. You can write your own by deriving from System.Windows.Markup.MarkupExtension.

Binding类是标记扩展。您可以通过从System.Windows.Markup.MarkupExtension派生来编写自己的。

ElementName and Path are simply properties on the Binding object.

ElementName和Path只是Binding对象的属性。

As for the followup you can create a new Binding in code by instantiating the Binding object. I do not know of a way to process a string through.

至于后续,您可以通过实例化Binding对象在代码中创建一个新的Binding。我不知道一种处理字符串的方法。

#1


2  

take a look at markupextensions http://blogs.msdn.com/wpfsdk/archive/2007/03/22/blogpost-text-creatingasimplecustommarkupextension.aspx

看看markupextensions http://blogs.msdn.com/wpfsdk/archive/2007/03/22/blogpost-text-creatingasimplecustommarkupextension.aspx

#2


1  

The Binding class is a Markup Extension. You can write your own by deriving from System.Windows.Markup.MarkupExtension.

Binding类是标记扩展。您可以通过从System.Windows.Markup.MarkupExtension派生来编写自己的。

ElementName and Path are simply properties on the Binding object.

ElementName和Path只是Binding对象的属性。

As for the followup you can create a new Binding in code by instantiating the Binding object. I do not know of a way to process a string through.

至于后续,您可以通过实例化Binding对象在代码中创建一个新的Binding。我不知道一种处理字符串的方法。