Click here to Skip to main content
12,699,022 members (25,792 online)
Click here to Skip to main content

Stats

99K views
3K downloads
40 bookmarked
Posted

Using the WPF FocusScope

, 26 Jul 2009 MIT
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

Share

About the Author

Daniel Grunwald
Germany Germany
I am the lead developer on the SharpDevelop open source project.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 27 Jul 2009
Article Copyright 2009 by Daniel Grunwald
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid