Get List of all Control Types in WPF

By | August 9, 2011

In this tips I am going to discuss how you can get list of all  WPF elements which are type of Controls and can be accessible publicly. To achieve this I have used reflection to hook the .NET API and get the list of Assembly elements which are types of Controls.

To implement this, first you have to search for all types in assembly where the control class is defined. Assembly.GetAssebly() takes a System.Type object and return the assembly in which the specified class is defined. image

Once we have the control Assembly object we can iterate through the each type and find for Control Type and which is not Abstract and a public. Below code snippet showing the same, where lisrofControls is nothing but  List<string>.

image

That’s all, final task is to define the ListBox ItemSource as ListofControls. Run the application, you get below output.

image

Complete XMAL Markup :

<Window x:Class="GetControls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="408" Width="402">
    <Grid Width="400" Height="400">	
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock HorizontalAlignment="Center" Name="txtTotalElement" Grid.Row="0" FontWeight="Bold" ></TextBlock>
        <ListBox Grid.Row="1" Name="lstControls" Margin="8,8,29,48"  BorderThickness="0,1,1,1" 
                >
        	<ListBox.Effect>
        		<DropShadowEffect BlurRadius="7" Color="#FFC46363" Opacity="0.45"/>
        	</ListBox.Effect>
        	<ListBox.Background>
        		<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
        			<GradientStop Color="Black"/>
        			<GradientStop Color="#FF18E0B7" Offset="1"/>
        			<GradientStop Color="#FFD6EBD8"/>
        		</LinearGradientBrush>
        	</ListBox.Background>
        </ListBox>
    </Grid>
</Window>