Click here to Skip to main content
15,878,814 members
Articles / Desktop Programming / WPF

Using the WPF FocusScope

Rate me:
Please Sign up or sign in to vote.
4.97/5 (22 votes)
26 Jul 2009MIT5 min read 154.4K   3.8K   46  
Explains why WPF seems to break if you try to use FocusScope, and provides a simple solution.
<Window x:Class="TestApp.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:t="clr-namespace:TestApp" Title="EnhancedFocusScope" Height="400" Width="500">
	<Window.Resources>
		<Style TargetType="GroupBox">
			<Setter Property="Background" Value="Transparent"/>
			<Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle"/>
			<Setter Property="KeyboardNavigation.ControlTabNavigation" Value="Once"/>
		</Style>
	</Window.Resources>
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="Auto" />
			<RowDefinition Height="100" />
			<RowDefinition />
		</Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition />
		</Grid.ColumnDefinitions>
		<ToolBar Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
			<Button Command="Copy">Copy</Button>
			<Button Command="Cut">Cut</Button>
			<Button Command="Paste">Paste</Button>
		</ToolBar>
		<TextBox Grid.Row="1" Grid.ColumnSpan="3" AcceptsReturn="True">A free text area</TextBox>
		<GroupBox Name="box1" Header="No focus scope" Grid.Row="2" MouseDown="Box1_MouseDown">
			<StackPanel Margin="4">
				<Button>a</Button>
				<Button>b</Button>
				<Button>c</Button>
				<TextBox />
				<CheckBox>CheckBox</CheckBox>
			</StackPanel>
		</GroupBox>
		<GroupBox Name="box2" Header="WPF focus scope" FocusManager.IsFocusScope="True" Grid.Column="1" Grid.Row="2" MouseDown="Box2_MouseDown">
			<StackPanel Margin="4">
				<Button>a</Button>
				<Button>b</Button>
				<Button>c</Button>
				<TextBox />
				<CheckBox>CheckBox</CheckBox>
			</StackPanel>
		</GroupBox>
		<GroupBox Name="box3" Header="My Focus Scope" t:EnhancedFocusScope.IsEnhancedFocusScope="True" Grid.Column="2" Grid.Row="2" MouseDown="Box3_MouseDown">
			<StackPanel Margin="4">
				<Button>a</Button>
				<Button>b</Button>
				<Button>c</Button>
				<TextBox />
				<CheckBox>CheckBox</CheckBox>
			</StackPanel>
		</GroupBox>
	</Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Germany Germany
I am the lead developer on the SharpDevelop open source project.

Comments and Discussions