如何按存储在字符串变量中的名称获取 XAML 元素



例如,我有一个UIElement:

<TextBlock Name="sometextblock" Text="sample text"/>

在代码中,我有一个具有该名称的字符串变量:

string elementName = "sometextblock";

如何使用此变量获取此元素?我需要访问元素的属性,例如,我需要能够更改 Text 属性。

怎么做?

谢谢!

如果在 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

假设你的 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;

您可以使用此方法:

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

您可以参考此方法:

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

最新更新