来自 ContentControl 的 DataTemplate 内的元素寻址
如何访问通过 ContentControl 显示的 DataTemplate 中包含的元素。 我有一个ContentControl,它托管一个PresentationModel,其方式如下:
<ContentControl x:Name="ContentContainer"
Content="{Binding}"
ContentTemplate="{Binding ContentControlTemplate, ElementName=this}"
其中“this”是视图(UserControl)。
我想在 DataGridControl 上 EndEdit,所以我尝试了以下方法:
ContentPresenter presenter = VisualTreeHelper.GetChild(this. ContentContainer, 0) as ContentPresenter;
DataGridControl dg = this. ContentContainer.ContentTemplate.FindName("datagrid", presenter) as DataGridControl;
dg.EndEdit();
问题是 ContentControl 没有子级,可能是因为内容绑定的方式?
我很感激任何帮助。
How do I access an element contained in a DataTemplate that is displayed through a ContentControl. I have a ContentControl which hosts a PresentationModel along the lines of:
<ContentControl x:Name="ContentContainer"
Content="{Binding}"
ContentTemplate="{Binding ContentControlTemplate, ElementName=this}"
Where "this" is the view (UserControl).
There's a DataGridControl I want to EndEdit on, so I tried this:
ContentPresenter presenter = VisualTreeHelper.GetChild(this. ContentContainer, 0) as ContentPresenter;
DataGridControl dg = this. ContentContainer.ContentTemplate.FindName("datagrid", presenter) as DataGridControl;
dg.EndEdit();
Problem is that the ContentControl has no children, maybe because of the way the content is bound?
I appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您正在将 GetChild 的结果投射到 ContentPresenter。 根据其模板,情况可能并非如此。 我认为它的默认模板包含一个边框,因此您的演员将返回 null。 如果除了显示内容之外不需要它做任何事情,为什么不直接使用ContentPresenter呢?
Well, you are casting the result of GetChild to a ContentPresenter. Depending on its Template, this may not be the case. I think its default template includes a Border, so your cast will return null. If you don't need it to do anything other than display the content, why not use ContentPresenter directly?