Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simplified TextBox focus in WPF 4

0.00/5 (No votes)
10 Sep 2010 1  
Simplified TextBox focus in WPF 4

While prepping for some session I have to present, I started revisiting WPF 4 and taking a deeper look at what's new! One of the new markup extensions I noticed is the {x:Reference …} markup extension!

“References an instance that is declared elsewhere in XAML markup. The reference refers to an element's x:Name.”

So, where is this used! One of the first scenarios that comes to mind is the focus target… In WPF 3.0/3.5, If I wanted to associate keyboard shortcut with setting focus on a TextBox, I needed to write the following code.

WPF 3.0/3.5

XML
<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Content="_Name:" Target="{Binding ElementName=nameTextBox}" 
               Grid.Row="0" Grid.Column="0" />
        <TextBox x:Name="nameTextBox" 
               Grid.Row="0" Grid.Column="1" Width="200" />
        <Label Content="_Surname:" Target="{Binding ElementName=surnameTextBox}" 
               Grid.Row="1" Grid.Column="0" />
        <TextBox x:Name="surnameTextBox" 
               Grid.Row="1" Grid.Column="1" Width="200" />
    </Grid>
</Window>

With WPF 4.0, this has become way easier… check this out.

WPF 4.0

XML
<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Content="_Name:" Target="{x:Reference nameTextBox}" 
               Grid.Row="0" Grid.Column="0" />
        <TextBox x:Name="nameTextBox" 
               Grid.Row="0" Grid.Column="1" Width="200" />
        <Label Content="_Surname:" Target="{x:Reference surnameTextBox}" 
               Grid.Row="1" Grid.Column="0" />
        <TextBox x:Name="surnameTextBox" 
               Grid.Row="1" Grid.Column="1" Width="200" />
    </Grid>
</Window>

And if the property you are binding to is marked with System.Windows.Markup.NameReferenceConverter:

XML
<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Content="_Name:" Target="nameTextBox" 
               Grid.Row="0" Grid.Column="0" />
        <TextBox x:Name="nameTextBox" 
               Grid.Row="0" Grid.Column="1" Width="200" />
        <Label Content="_Surname:" Target="surnameTextBox" 
               Grid.Row="1" Grid.Column="0" />
        <TextBox x:Name="surnameTextBox" 
               Grid.Row="1" Grid.Column="1" Width="200" />
    </Grid>
</Window>

[NOTE] If you try this in WPF 3.5, you will get a “'UIElement' type does not have a public TypeConverter class.” exception message.

Simple, isn’t it?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here