gpt4 book ai didi

c# - 为什么我不能使用 FindName() 按名称访问文本框?

转载 作者:太空狗 更新时间:2023-10-29 20:57:50 24 4
gpt4 key购买 nike

为什么 FindName() 在以下示例中返回 null

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>

代码隐藏:

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);
}
}
}

答案附录:

非常感谢,Bruno,效果很好。为了不重复添加相同的名称,我用这个包裹它:

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

或者,如果您要注册除 TextBox 之外的任何其他内容,则为通用版本:

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

最佳答案

这与 WPF XAML Namescopes 有关.

因为向解析后的元素树中添加元素,所以需要调用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.

关于c# - 为什么我不能使用 FindName() 按名称访问文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1755377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com