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

问题描述 投票:2回答:3

例如,我有一个UIElement。

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

在代码中,我有一个字符串变量的名字。

string elementName = "sometextblock";

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

如何做到这一点?

谢谢你!请问如何使用这个变量来获取这个元素的属性?

c# xaml windows-phone-8
3个回答
8
投票

如果你在你的XAML中对元素进行了如下命名。

<TextBlock x:Name="sometextblock" />

你可以通过 查找名称 方法。

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


string elementName = txt.xyzproperty //do what you want with using txt.xyz property

0
投票

你可以用这个方法,

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

0
投票

假设你的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;
© www.soinside.com 2019 - 2024. All rights reserved.