【问题标题】:With WPF, how to retrieve the controls contained by a DataTemplate?使用 WPF,如何检索 DataTemplate 包含的控件?
【发布时间】:2009-10-02 06:55:22
【问题描述】:

基本上我有一个包含 Border、StackPanel 和其他东西的 DataTemplate,后来我在许多 ContentControl(s) 中将此 DataTemplate 用作 ContentTemplate。

这些 ContentControl(s) 已命名,但从 C# 代码中我无法找到从它们那里取回我的 Border、StackPanel 和内容的方法。

有什么想法吗?

【问题讨论】:

    标签: c# wpf datatemplate


    【解决方案1】:

    你应该可以这样做:

    // Finding textBlock from the DataTemplate that is set on that ContentPresenter
    DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
    TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
    

    您可以在这里找到更多信息:How to: Find DataTemplate-Generated Elements

    【讨论】:

    【解决方案2】:

    试试FindResource()

    here's an example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      相关资源
      最近更新 更多