Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created wpf user control and set triggers on GotFouce and LostFouce of user control.
and checked foucsable property . but , not set fouce on user contol when press tab ...

please help me to find the problem


<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	x:Class="Test2.UserControl1" Height="89" Width="239" Focusable="True" TabIndex="0">
	<UserControl.Resources>
		<Storyboard x:Key="OnMouseEnter1">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="path">
				<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.4"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
	</UserControl.Resources>
	<UserControl.Triggers>
		<EventTrigger RoutedEvent="FocusManager.GotFocus">
			<BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter1}"/>
		</EventTrigger>
		<EventTrigger RoutedEvent="FocusManager.LostFocus">
			<RemoveStoryboard BeginStoryboardName="OnMouseEnter1_BeginStoryboard"/>
		</EventTrigger>
	</UserControl.Triggers>

	<Grid x:Name="LayoutRoot">
		<Border x:Name="border" BorderBrush="Black" BorderThickness="1" Height="88" VerticalAlignment="Top" >
			<Border.Background>
				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
					<GradientStop Color="#FF2F2E2E" Offset="0.515"/>
					<GradientStop Color="#FF8D8D8D" Offset="1"/>
					<GradientStop Color="#FF707070"/>
				</LinearGradientBrush>
			</Border.Background>
			<Path x:Name="path" Data="M13.645051,-0.30730114 L15.397623,69.760567 220.46757,70.447496 232.538,33.97507 217.71921,0.37365396" Margin="3.368,3.373,13.748,10.927" Stretch="Fill" RenderTransformOrigin="0.5,0.5" Opacity="0">
				<Path.RenderTransform>
					<TransformGroup>
						<ScaleTransform/>
						<SkewTransform/>
						<RotateTransform Angle="-180.193"/>
						<TranslateTransform X="1.6283292487494805" Y="4.2976017301465959"/>
					</TransformGroup>
				</Path.RenderTransform>
				<Path.Effect>
					<DropShadowEffect Color="#FFE0E0E0" ShadowDepth="2" Opacity="0.6"/>
				</Path.Effect>
				<Path.Fill>
					<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
						<GradientStop Color="#FFFFE998" Offset="0.478"/>
						<GradientStop Color="#FFFFFDF6" Offset="1"/>
						<GradientStop Color="#FFFBFAF8"/>
					</LinearGradientBrush>
				</Path.Fill>
			</Path>
			
		</Border>
		<TextBlock x:Name="txtblk"  VerticalAlignment="Center" HorizontalAlignment="Center" Text="YourText"/>
	</Grid>
</UserControl>
Posted
Updated 7-Aug-12 7:14am
v4
Comments
[no name] 7-Aug-12 12:52pm    
Help you find the problem with your code that we cannot see?
Sergey Alexandrovich Kryukov 7-Aug-12 13:08pm    
I already warned OP about asking non-questions, bad questions, and, if my memory does not betray me, even deleted some re-posts. Let's consider this as another warning, but this time it's possible to give some answer, so I did -- please see.
--SA

1 solution

The Tab browsing mechanism is already embedded in the library; you don't have to do anything to engage it. [EDIT] If, by some reason, this mechanism does not work in your code, it would mean you screw up it somehow. If this is the case, you would need to create a complete but very short code sample to reproduce the problem and show it using "Improve question". [END EDIT]

If you need to do focusing by your code by other means, it's good to learn how focusing works in WPF. It is not so trivial as you might think:
http://msdn.microsoft.com/en-us/library/aa969768.aspx[^].

The WPF advance in this field is that it preserves the logical focus as opposed to the "real" keyboard focus of an element.

Nevertheless, in many cases, simple System.Windows.UIElement.Focus could suffice:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.focus.aspx[^].

You only need to understand different notions of the focus and what functionality is involved with them.

Even though the documentation says it only "attempts to set focus to this element", you can easily foresee the situations where is would be always successful. If the focus was not forced to change, you will be notified through the Boolean return value.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900