Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / WPF

Simplified TextBox focus in WPF 4

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
10 Sep 2010CPOL 16.4K   4   3
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Pieter Muller13-Sep-10 20:27
Pieter Muller13-Sep-10 20:27 
GeneralRe: My vote of 2 Pin
rudigrobler13-Sep-10 22:22
rudigrobler13-Sep-10 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.