How to find a control with a specific name in an XAML UI with C# code?

喜夏-厌秋 提交于 2020-01-01 11:47:48

问题


I have dynamic added controls in my XAML UI. How I can find a specific control with a name.


回答1:


There is a way to do that. You can use the VisualTreeHelper to walk through all the objects on the screen. A convenient method I use (obtained it somewhere from the web) is the FindControl method:

public static T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
{

    if (parent == null) return null;

    if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
    {
        return (T)parent;
    }
    T result = null;
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);

        if (FindControl<T>(child, targetType, ControlName) != null)
        {
            result = FindControl<T>(child, targetType, ControlName);
            break;
        }
    }
    return result;
}

You can use it like this:

var combo = ControlHelper.FindControl<ComboBox>(this, typeof(ComboBox), "ComboBox123");



回答2:


I have extended @Martin Tirion version, to make it comfortable:

  • Eliminate the type parameter
  • Make UIElement extension for better use

Here is the changed code:

namespace StackOwerflow.Sample.Helpers
{
    public static class UIElementExtensions
    {
        public static T FindControl<T>( this UIElement parent, string ControlName ) where T : FrameworkElement
        {
            if( parent == null )
                return null;

            if( parent.GetType() == typeof(T) && (( T )parent).Name == ControlName )
            {
                return ( T )parent;
            }
            T result = null;
            int count = VisualTreeHelper.GetChildrenCount( parent );
            for( int i = 0; i < count; i++ )
            {
                UIElement child = ( UIElement )VisualTreeHelper.GetChild( parent, i );

                if( FindControl<T>( child, ControlName ) != null )
                {
                    result = FindControl<T>( child, ControlName );
                    break;
                }
            }
            return result;
        }
    }
}

After the modification, I am able to use like this:

var combo = parent.FindControl<ComboBox>("ComboBox123");

or when the parent is the current dialog it just like this:

var combo = FindControl<ComboBox>("ComboBox123");

Thank you @Martin Tirion again!




回答3:


When you create the Control in XAML you can give it a x:Name="..." Tag. In your corresponding C# Class the Control will be available under that name.
Some Container Views like Grid got a Children Property, you can use this to search for Controls inside them.



来源:https://stackoverflow.com/questions/38110972/how-to-find-a-control-with-a-specific-name-in-an-xaml-ui-with-c-sharp-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!