C# 如何按名称获取存储在字符串变量中的XAML元素?
例如,我有一个UIElement:C# 如何按名称获取存储在字符串变量中的XAML元素?,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,例如,我有一个UIElement: <TextBlock Name="sometextblock" Text="sample text"/> 如何使用此变量获取此元素?我需要访问元素的属性,例如,我需要能够更改文本属性 如何做到这一点 谢谢大家! 您可以使用以下方法: var textBlock = FindChild<TextBlock>(Application.Current.RootVisual, "sometextblock"); public sta
<TextBlock Name="sometextblock" Text="sample text"/>
如何使用此变量获取此元素?我需要访问元素的属性,例如,我需要能够更改文本属性
如何做到这一点
谢谢大家! 您可以使用以下方法:
var textBlock = FindChild<TextBlock>(Application.Current.RootVisual, "sometextblock");
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;
}
var textBlock=FindChild(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;
}
}
public static T FindChild(DependencyObject父对象,字符串childName)
其中T:DependencyObject
{
//确认父项和子项名称有效。
如果(父项==null)
{
返回null;
}
T foundChild=null;
int childrenCount=visualtreeheloper.GetChildrenCount(父级);
for(int i=0;i
如果您在XAML中对元素进行了如下命名:
<TextBlock x:Name="sometextblock" />
假设您在XAML上有此功能:
<Button Name="MyButton1" .../>
<Button Name="MyButton2" .../>
<Image Name="MyImage1" .../>
<TextBox Name="MyTextBox1" .../>
<TextBox Name="MyTextBox2" .../>
<Button Name="MyButton3" .../>
然后可以迭代控件并访问属性:
for (int i = 0; i < NameControls.Length; i++)
{
dynamic MyControl = this.FindName(NameControls[i]);
// do something
}
您可以参考此方法:
var textBlock = FindChild<TextBlock>(Application.Current.RootVisual, "sometextblock");
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;
}
public static bool FindVisualChildByName(此DependencyObject父对象,字符串名称,不受控制),其中T:DependencyObject
{
如果(父项==null)
抛出新ArgumentNullException(nameof(parent),“控件c”ấp cha khđongưược无效。”);
if(string.IsNullOrWhiteSpace(name))
抛出新ArgumentNullException(nameof(name),“Tên của对照组cần tìm khđngượ何志平ặc空。”);
var childrenCount=visualtreeheloper.GetChildrenCount(父级);
对于(变量i=0;i
还有更简单的方法吗?
dynamic MyControl = this.FindName(NameControls[i]);
MyControl.Opacity = 0.7;
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;
}