Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

uwp

uwp-xaml

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

like image 696
CorneDompelaar Avatar asked Jun 29 '16 22:06

CorneDompelaar


People also ask

How to Find Control by Name in WPF?

FindName method of FrameworkElement class is used to find elements or controls by their Name properties. This article shows how to find controls on a Window by name. FindName method of FrameworkElement class is used to find elements or controls by their Name properties.

How do we refer to WPF controls in code?

Controls in WPF are accessed by their names (and the Name property). We specify the Name property in the XAML, and then can access the control by that name directly in C# code. This allows controls to interact. Example.

What is a control in XAML?

The Control class is the base class for many of the controls you add to an app and its UI. The Control class defines very little behavior; you can add an actual Control element to XAML for UI but you typically add something that inherits from Control directly or indirectly, such as a Button or ListBox.

What is a XAML page?

Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.


2 Answers

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");
like image 54
Martin Tirion Avatar answered Oct 05 '22 00:10

Martin Tirion


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!

like image 45
György Gulyás Avatar answered Oct 05 '22 00:10

György Gulyás