如何按存储在字符串变量中的名称获取 XAML 元素?
Posted
技术标签:
【中文标题】如何按存储在字符串变量中的名称获取 XAML 元素?【英文标题】:How to get XAML element by name stored in a string variable? 【发布时间】:2014-03-30 07:49:12 【问题描述】:例如,我有一个 UIElement:
<TextBlock Name="sometextblock" Text="sample text"/>
在代码中,我有一个具有该名称的字符串变量:
string elementName = "sometextblock";
如何获取这个元素,使用这个变量?我需要访问元素的属性,例如,我需要能够更改 Text 属性。
如何做到这一点?
谢谢!
【问题讨论】:
【参考方案1】:你可以参考这个方法:
public static bool FindVisualChildByName<T>(this DependencyObject parent, string name, out T control) where T : DependencyObject
if (parent == null)
throw new ArgumentNullException(nameof(parent), "Control cấp cha không được null.");
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name), "Tên của control cần tìm không được null hoặc empty.");
var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childrenCount; i++)
var child = VisualTreeHelper.GetChild(parent, i);
var controlName = child.GetValue(FrameworkElement.NameProperty) as string;
if (controlName == name)
control = child as T;
return true;
if (FindVisualChildByName(child, name, out control)) return true;
control = null;
return false;
【讨论】:
【参考方案2】:假设你的 XAML 上有这个:
<Button Name="MyButton1" .../>
<Button Name="MyButton2" .../>
<Image Name="MyImage1" .../>
<TextBox Name="MyTextBox1" .../>
<TextBox Name="MyTextBox2" .../>
<Button Name="MyButton3" .../>
您必须将控件的名称放在一个字符串数组中,所以:
string[] NameControls = "MyButton1", "MyButton2", "MyImage1", "MyTextBox1", "MyTextBox2", "MyButton3" ;
然后你可以迭代控件并访问属性:
for (int i = 0; i < NameControls.Length; i++)
dynamic MyControl = this.FindName(NameControls[i]);
// do something
例如,在我的情况下,我需要更改已确定控件的不透明度,因此,在 for 块中我已将其放入:
dynamic MyControl = this.FindName(NameControls[i]);
MyControl.Opacity = 0.7;
【讨论】:
【参考方案3】:如果您在 XAML 中按如下方式命名元素:
<TextBlock x:Name="sometextblock" />
您可以通过FindName 方法找到它们:
TextBlock txt = this.FindName("sometextblock") as TextBlock;
string elementName = txt.xyzproperty //do what you want with using txt.xyz property
【讨论】:
【参考方案4】:你可以用这个方法:
var textBlock = FindChild<TextBlock>(Application.Current.RootVisual, "sometextblock");
FindChild 方法是:
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
// Confirm parent and childName are valid.
if (parent == null)
return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
var childType = child as T;
if (childType == null)
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null)
break;
else if (!string.IsNullOrEmpty(childName))
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
// if the child's name is of the request name
foundChild = (T) child;
break;
// Need this in case the element we want is nested
// in another element of the same type
foundChild = FindChild<T>(child, childName);
else
// child element found.
foundChild = (T) child;
break;
return foundChild;
【讨论】:
有更简单的方法吗?以上是关于如何按存储在字符串变量中的名称获取 XAML 元素?的主要内容,如果未能解决你的问题,请参考以下文章
获取存储在变量中的字符串的值,其名称作为字符串存储在另一个变量中