【问题标题】:Why doesnt Window.FindName() discover the x:Name of a button in a child UserControl? AKA how do NameScopes work?为什么 Window.FindName() 没有发现子 UserControl 中按钮的 x:Name? AKA NameScopes 是如何工作的?
【发布时间】:2010-12-18 08:57:44
【问题描述】:

所以在下面的示例代码中,我创建了一个 UserControl UserControldChild,它是主窗口 Window1.xaml 的子窗口。为什么FindName() 方法在下面的代码中找不到“myButton”?

这一定与WPF XAML NameScopes 有关,但我还没有找到关于 NameScope 如何工作的好的解释。有人可以启发我吗?

//(xml) Window1.xaml    
<Window x:Class="VisualTreeTestApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:VisualTreeTestApp="clr-namespace:VisualTreeTestApplication"
    Title="Window1" Height="400" Width="400">
    <Grid>
        <VisualTreeTestApp:UserControlChild/>
    </Grid>
</Window>

//(c#) Window1.xaml.cs
namespace VisualTreeTestApplication
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      Button btnTest = (Button)Application.Current.MainWindow.FindName("myButton");
      // btnTest is null!
    }
  }
}

下面的用户控件:

//(wpf) UserControlChild.xaml
<UserControl x:Class="VisualTreeTestApplication.UserControlChild"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid x:Name="myGrid">      
        <Button x:Name="myButton" Margin="20" >Button</Button>
    </Grid>
</UserControl>

//(c#) UserControlChild.xaml.cs (no changes)
namespace VisualTreeTestApplication
{
  /// <summary>
  /// Interaction logic for UserControlChild.xaml
  /// </summary>
  public partial class UserControlChild : UserControl
  {
    public UserControlChild()
    {
      InitializeComponent();
    }
  }
}

如果没有得到正确回答,我找到了使用 FindName() 记录的in the post here 的替代方法。

【问题讨论】:

    标签: c# wpf user-controls findname


    【解决方案1】:

    您是正确的 - 这与 XAML 名称范围有关。

    Name related APIs section of the XAML Namescopes page 中记录了这一点(有些糟糕)。

    基本上,如果您有一个 FrameworkElement 或 FrameworkContentElement,它将定义自己的名称范围。如果您对没有名称范围的类型调用 FindName(),WPF 会向上搜索该树直到找到确实定义了名称范围的元素,然后在该名称范围内进行搜索。

    在您的情况下,它在 Window 的名称范围内进行搜索(它是一个 FrameworkContentElement,因此它定义了自己的范围)。它只搜索在该范围内定义的元素。

    在您的情况下,该按钮位于 UserControl 的名称范围内,因此 Window.FindName() 找不到它。没有自动搜索向下树到较低级别的范围。

    这是一件好事——你的“窗口”不应该知道或不想知道它正在使用的 UserControl 的内部细节。如果您需要 UserControl 中的属性,它们应该在 UserControl 级别公开 - 让控件管理自己的子控件。

    【讨论】:

    • 感谢您的澄清!在这种情况下,FindName 似乎不是工作的最佳工具,也就是说,您不能轻易地在范围内搜索。除了这里详述的递归解决方案之外,你能想出一个更好的方法来在可视化树中找到子按钮的名称吗? stackoverflow.com/questions/636383/wpf-ways-to-find-controls/…
    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多