如何实现内部内容依赖属性?
我正在尝试实现具有依赖属性的用户控件。这是我的问题;我想用用户控件的一个或多个布局子项设置依赖属性。这可能吗?怎样才能做到?
<custom:myControl1>
<Label>Controls</Label>
<Label>I want</Label>
<Label>to set</Label>
<Label>as the dependency property</Label>
<Button Content="Is it possible?" />
</custom:myControl1>
I'm trying to implement a usercontrol with dependency properties. Here is my question; i want to set a dependency property with layout child or children of my user control. Is it possible and how can it be done?
<custom:myControl1>
<Label>Controls</Label>
<Label>I want</Label>
<Label>to set</Label>
<Label>as the dependency property</Label>
<Button Content="Is it possible?" />
</custom:myControl1>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,在
UserControl
的 XAML 中声明ContentControl
。让它将其
Content
属性绑定到UserControl
代码隐藏的DependencyProperty
。在 UserControl 类顶部添加属性:
[ContentProperty("Name_Of_Your_Dependency_Property")]
。然后你就可以完全按照你在问题中所做的那样做。该属性定义默认的依赖属性,以便您不必指定
。像这样的东西:
Yes, declare a
ContentControl
in the XAML of yourUserControl
.Make it bind its
Content
property to aDependencyProperty
on the code-behind of yourUserControl
.Add the attribute:
[ContentProperty("Name_Of_Your_Dependency_Property")]
on top of your UserControl class.Then you can do precisely as you did in your question. The attribute defines the default Dependency Property so that you dont have to specify
<custom:myControl1.MyDP>
.Something like:
按预期工作。
Working as expected.