为什么我不能按名称访问一个TextBox与FindName()? [英] Why can't I access a TextBox by Name with FindName()?

查看:255
本文介绍了为什么我不能按名称访问一个TextBox与FindName()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 FindName()返回的无效在下面的例子吗?

XAML:

 <窗​​口x:类=TestDynamicTextBox343.Window1
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    标题=窗口1HEIGHT =300WIDTH =300>
    <&StackPanel的GT;
        < BORDER>            < D​​ockPanel中X:NAME =FormBaseLastChildFill =真>            < / DockPanel中>        < /边框>        <按钮内容=保存点击=Button_Click/>
    < / StackPanel的>
< /窗GT;

code背后:

 使用系统;
使用System.Windows;
使用System.Windows.Controls的;命名空间TestDynamicTextBox343
{
    公共部分类窗口1:窗口
    {
        公共窗口1()
        {
            的InitializeComponent();
            StackPanel的SP =新的StackPanel();
            sp.Orientation = Orientation.Horizo​​ntal;            TextBlock的文本块=新的TextBlock();
            textBlock.Text =名字:;            文本框的textBox =新的TextBox();
            textBox.Name =名字;
            textBox.Text =测试;            sp.Children.Add(文字块);
            sp.Children.Add(的textBox);
            FormBase.Children.Add(SP);        }        私人无效Button_Click(对象发件人,RoutedEventArgs E)
        {
            文本框TB =(文本框)this.FindName(名字);
            Console.WriteLine(tb.Text);
        }
    }
}

补遗答:

非常感谢,布鲁诺,运行良好。为了不添加相同的名称两次,我这个包起来:

 无效RegisterTextBox(字符串textBoxName,文本框的textBox)
{
    如果((文本框)this.FindName(textBoxName)!= NULL)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName,textBox中);
}

或者如果你将被登记除文本框以外的任何一个通用版本:

 无效RegisterControl< T>(串textBoxName,T的textBox)
{
    如果((T)this.FindName(textBoxName)!= NULL)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName,textBox中);
}


解决方案

这就是 WPF XAML名称范围。

由于添加元素分析元素的树木,你需要调用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.markup.inamescope.registername.aspx\"><$c$c>RegisterName.

  ...
        文本框的textBox =新的TextBox();
        textBox.Name =名字;
        textBox.Text =测试;        this.RegisterName(名字的textBox);
        ...


  

添加元素解析的元素
  树


  
  

任何增加的元素树
  初始加载和处理后,
  必须调用相应的
  实施RegisterName为
  类定义XAML名称范围。
  否则,所添加的对象不能
  按名称通过方法引用
  如FindName。仅仅设置
  Name属性(或X:名称属性)
  不注册该名称到任何
  XAML名称范围。添加命名元素
  到具有XAML元素树
  名称范围还没有注册
  命名为XAML名称范围。虽然
  XAML名称范围可以嵌套,你
  一般注册名称为XAML
  这根存在名称范围
  元素,让你的XAML名称范围
  位置平行于XAML名称范围
  将已在一个已创建
  相当于加载XAML页面。最多
  应用常见的场景
  开发商是您将使用
  RegisterName为名称注册成
  在当前根XAML名称范围
  页面。 RegisterName是一部分
  为找到一个重要的场景
  将运行作为故事板
  动画。欲了解更多信息,请参阅
  演示图板概述。如果调用
  RegisterName以外的元件上
  在同一个对象的根元素
  树,名字仍然登记
  最近的根元素,仿佛
  您曾呼吁RegisterName上
  根元素。


Why does FindName() return null in the following example?

XAML:

<Window x:Class="TestDynamicTextBox343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Border >

            <DockPanel x:Name="FormBase" LastChildFill="True">

            </DockPanel>

        </Border>

        <Button Content="Save" Click="Button_Click"/>
    </StackPanel>
</Window>

Code Behind:

using System;
using System.Windows;
using System.Windows.Controls;

namespace TestDynamicTextBox343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();


            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "First Name: ";

            TextBox textBox = new TextBox();
            textBox.Name = "FirstName";
            textBox.Text = "test";

            sp.Children.Add(textBlock);
            sp.Children.Add(textBox);
            FormBase.Children.Add(sp);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = (TextBox)this.FindName("FirstName");
            Console.WriteLine(tb.Text);
        }
    }
}

Addendum to Answer:

Thanks a lot, Bruno, that worked well. In order not to add the same name twice, I wrap it with this:

void RegisterTextBox(string textBoxName, TextBox textBox)
{
    if ((TextBox)this.FindName(textBoxName) != null)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName, textBox);
}

Or if you will be registering anything other than TextBoxes, a generic version:

void RegisterControl<T>(string textBoxName, T textBox)
{
    if ((T)this.FindName(textBoxName) != null)
        this.UnregisterName(textBoxName);
    this.RegisterName(textBoxName, textBox);
}

解决方案

This is related to WPF XAML Namescopes.

Because you add elements to parsed element trees, you need to call RegisterName.

        ...
        TextBox textBox = new TextBox();
        textBox.Name = "FirstName";
        textBox.Text = "test";

        this.RegisterName("FirstName", textBox);
        ...

Adding Elements to Parsed Element Trees

Any additions to the element tree after initial loading and processing must call the appropriate implementation of RegisterName for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name through methods such as FindName. Merely setting a Name property (or x:Name Attribute) does not register that name into any XAML namescope. Adding a named element to an element tree that has a XAML namescope also does not register the name to the XAML namescope. Although XAML namescopes can be nested, you generally register names to the XAML namescope that exists on the root element, so that your XAML namescope location parallels the XAML namescope that would have been created in an equivalent loaded XAML page. The most common scenario for application developers is that you will use RegisterName to register names into the XAML namescope on the current root of the page. RegisterName is part of one important scenario for finding storyboards that will run as animations. For more information, see Storyboards Overview. If you call RegisterName on an element other than the root element in the same object tree, the name is still registered to the element nearest the root, as if you had called RegisterName on the root element.

这篇关于为什么我不能按名称访问一个TextBox与FindName()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆