来自 ContentControl 的 DataTemplate 内的元素寻址

发布于 2024-07-30 07:26:41 字数 704 浏览 4 评论 0原文

如何访问通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2024-08-06 07:26:41

好吧,您正在将 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文