Here I will post problems I and my colleagues met and solutions we found.

Friday, January 21, 2011

Default focus in XAML

Very simple task, you open window and you want some text box to have focus. And since it's common and simple task I thought it should be possible using XAML. To do this, you should use FocusManager. However, you should pay attention where you put it. I had to use it right in the Grid where I placed my TextBox. Which is logical, if you think about it. How else TextBox can be found.

Here is the xaml:



<UserControl x:Class="Actsoft.CometTracker.Layouts.AddViewView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="40" d:DesignWidth="300"
>
<Grid
FocusManager.FocusedElement="{Binding ElementName=textBox}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label
Margin="4">View Name:</Label>
<TextBox Grid.Column="1" x:Name="textBox"
Margin="4" Text="{Binding Path=ViewName}"/>
</Grid>
</UserControl>

No comments: