如何在 XAML 中将默认属性设为 Content 以外的内容?

Jam*_* Ko 5 c# xaml win-universal-app windows-10 uwp

我是 Windows 应用程序开发新手。我正在创建一个UserControl其他用户可以在他们的应用程序中使用的自定义项。我想配置我的控件,以便如果您在其中放置任何内容,它将分配给Content. 为了给你更好的解释,如果你这样写:

<Button>Hello, world!</Button>
Run Code Online (Sandbox Code Playgroud)

这相当于你写了这样的内容:

<Button Content="Hello, world!"/>
Run Code Online (Sandbox Code Playgroud)

我可以让 XAML 解析器将控件的内容设置为除Content,还是不可能?

谢谢。

rom*_*oza 3

您可以使用 ContentPropertyAttribute:

[ContentProperty("DefaultProperty")]
Run Code Online (Sandbox Code Playgroud)

更多信息请访问:https://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute.aspx