WPF DataTemplate 显示原始控件
在数据模板中,我想显示实际上被数据模板替换的控件。通过错误验证,可以使用
实现这一点。但是,这似乎不适用于普通的 DataTemplate。
我认为会有一个简单的解决方案,但就是找不到。
多谢
In a DataTemplate I want to show the Control which is actually replaced by the DataTemplate. With error validation, this is possible by using <AdornedElementPlaceHolder />
. However, this seems not to work in a normal DataTemplate.
I think there would be a simple solution, but just can't find it.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,原因如下。
DataTemplate
不会替换任何内容。相反,定义 DataTemplate 的控件包含它提供的内容。如果可能的话,就会出现无限循环(控制 -> 数据模板 -> 控制 -> 数据模板 -> ...)。我建议您阅读以下文章以完全理解数据模板:http://msdn。 microsoft.com/en-us/library/ms742521.aspx
This is not possible and here is why.
DataTemplate
doesn't replace anything. Instead, a control that defines a DataTemplate contains the content provided by it. If it was possible then there would be an infinite loop (control -> data template -> control -> data template -> ...).I suggest you read the following article to fully understand data templates: http://msdn.microsoft.com/en-us/library/ms742521.aspx
从某种意义上说,DataTemplate“替换”了数据对象,而不是UI对象——换句话说,不是控件。如果您尝试使用使用模板的控件(通常是 ContentControl 或 ItemsControl)的属性,请尝试 相对源绑定,其中 AncestorType 是控件的类型。
A DataTemplate in some sense "replaces" a data object and not a UI object - in other words, not a Control. If you are trying to work with properties of the Control (usually either a ContentControl or ItemsControl) that is using the template, try a RelativeSource Binding where the AncestorType is the type of the Control.