How to get XAML element by name stored in a string variable?
Asked Answered
F

4

8

For example, I have an UIElement:

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

In the code I have a string variable with that name:

string elementName = "sometextblock";

How to get this element, using this variable? I need to get access to element's properties, for example, i need to be able to change a Text property.

How to do this?

Thank you!

Fraga answered 27/2, 2014 at 4:31 Comment(0)
R
14

If you have named elements in your XAML as follows:

<TextBlock x:Name="sometextblock" />

You can find them via the FindName method:

TextBlock txt = this.FindName("sometextblock") as TextBlock;


string elementName = txt.xyzproperty //do what you want with using txt.xyz property
Rosario answered 27/2, 2014 at 4:42 Comment(0)
S
1

Assuming you've this on your XAML:

<Button Name="MyButton1" .../>
<Button Name="MyButton2" .../>
<Image Name="MyImage1" .../>
<TextBox Name="MyTextBox1" .../>
<TextBox Name="MyTextBox2" .../>
<Button Name="MyButton3" .../>

You must put the name of your controls inside an array of string, so:

string[] NameControls = { "MyButton1", "MyButton2", "MyImage1", "MyTextBox1", "MyTextBox2", "MyButton3" };

Then you can iterate the controls and access the properties:

for (int i = 0; i < NameControls.Length; i++)
{
    dynamic MyControl = this.FindName(NameControls[i]);
    // do something
}

For example in my case I need to change the Opacity of determinated controls so, inside the for block I've put this:

dynamic MyControl = this.FindName(NameControls[i]);
MyControl.Opacity = 0.7;
Salmi answered 10/5, 2020 at 21:12 Comment(0)
B
0

you can use this method:

var textBlock = FindChild<TextBlock>(Application.Current.RootVisual, "sometextblock");

and the FindChild method is :

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;
    }
}
Barbera answered 27/2, 2014 at 4:35 Comment(1)
Is there a more simple way?Fraga
B
0

You can refer to this method:

    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;
    }
Baylor answered 5/7, 2020 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.