【问题标题】:Is there a workaround for the missing FindName method in Silverlight's DataTemplate?Silverlight 的 DataTemplate 中缺少 FindName 方法是否有解决方法?
【发布时间】:2010-09-22 05:36:34
【问题描述】:

根据 C# 编译器和 Silverlight 2 文档,Silverlight 不为 DataTemplate 类提供 FindName 方法。我想在 ContentPresenter 中找到一个边框。 SilverLight 2 中的最佳方式是什么?

【问题讨论】:

    标签: silverlight datatemplate findname


    【解决方案1】:

    不完全确定我理解这个场景,但既然你提到了 DataTemplate,我假设你正在使用模板。

    如果您使用的是模板,那么您所做的就是为您的边框命名 (x:Name="border"),然后覆盖 OnApplyTemplate 方法。在该方法中,您使用 GetTemplateChild 并传递您使用的名称。这将返回对您的边框的引用。

    如果您没有使用模板并且引用了 ContentPresenter,那么您可以编写一个递归函数来查看子项的 Content 属性,如果它不是边框,则在其内容上调用相同的函数.

    【讨论】:

    • 这适用于 ControlTemplate,但我相信你不能在 DataTemplate 中真正做到这一点。
    【解决方案2】:

    如果边框在 DataTemplate 内,而不是 ControlTemplate 内,那么我过去能够做到这一点的唯一方法是使用 VisualTreeHelper 来定位我需要的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-07
      • 2023-03-30
      • 2011-11-18
      • 2016-10-19
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多