I have dynamic added controls in my XAML UI. How I can find a specific control with a name.
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.
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.
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.
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.
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");
I have extended @Martin Tirion version, to make it comfortable:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With