Silverlight - 按钮控制制表符停止两次之谜
我正在调试一个大型 Silverlight 应用程序,我发现在每个表单上,所有按钮控件在转到下一个控件之前都会接收选项卡焦点两次。但我无法在简单的 hello world 应用程序中重现此行为。有趣的是,这个问题不会发生在相同形式的 HyperlinkButton 控件上,只是 Button 上。知道什么会导致这种情况,或者有一些东西可以帮助我找出造成这种情况的原因吗?
提前致谢。
[编辑]
如果我将 IsTabStop 属性设置为 false,那么它只会在按钮上按 Tab 键一次。我犹豫是否使用它作为解决方案,因为我真的很想知道问题的根源是什么。
我已将问题追溯到我们在资源文件中使用的隐式按钮样式。我真的很想知道为什么这个 XAML 会导致这种情况发生。
隐式按钮样式 XAML
<Style TargetType="Button" x:Key="DefaultButtonStyle">
<Setter Property="Background" Value="{StaticResource NormalBrush}"/>
<Setter Property="Foreground" Value="{StaticResource TextContentBrush}"/>
<Setter Property="FontFamily" Value="{StaticResource ContentFontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource ContentFontSize}"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Padding" Value="10,3,10,3" />
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1" To="Pressed">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseIn" Exponent="-2"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="Pressed" GeneratedDuration="0:0:0.1" To="MouseOver">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseOut" Exponent="0"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="Normal" GeneratedDuration="0:0:0.01" To="MouseOver">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseIn" Exponent="7"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1" To="Normal">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource BaseColor2}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="PressedBorder" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1.0" KeySpline="0,0,0.0299999993294477,0.920000016689301"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource BaseColor2}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.75"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.3"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused" >
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="BaseBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="1" >
<Border.Background>
<SolidColorBrush Color="{StaticResource BaseColor7}"/>
</Border.Background>
<Grid>
<Border x:Name="Background" BorderBrush="{StaticResource NormalInnerBorderBrush}" Background="{TemplateBinding Background}"/>
<Border x:Name="MouseOverBorder" Opacity="0" Background="{StaticResource MouseOverBrush}" Margin="-0.5,-0.5,0.5,0.5"/>
<Border x:Name="PressedBorder" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" Background="{StaticResource MouseOverBrush}" RenderTransformOrigin="0.5,0.5" Margin="-1">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</Grid>
</Border>
<Rectangle x:Name="DisabledVisualElement" Fill="{StaticResource DisabledBackgroundBrush}" IsHitTestVisible="false" Opacity="0" RadiusY="1" RadiusX="1" Stroke="{StaticResource DisabledBackgroundBrush}"/>
<ContentControl x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Foreground="{TemplateBinding Foreground}" Padding="2" />
<Rectangle x:Name="FocusVisualElement" Stroke="{StaticResource FocusedBrush}" Margin="-1" Opacity="0" /> <!--IsHitTestVisible="false"-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'm debugging a large Silverlight application and i'm finding on every form all button controls receive tab focus twice before going to next control. I cannot reproduce this behavior in a simple hello world application though. Interestingly this problem does not occur on HyperlinkButton controls in the same form, just Button. Any idea what would cause this, or some things to help me isolate the cause of this?
Thanks in advance.
[Edit]
If I set the IsTabStop property to false then it just tabs on the button one time. I hesitate to use this as a fix as I really would like to know what the source of the problem is.
I've traced the problem to the Implicit button style that we're using in a resource file. I would really like to know why this XAML causes this to happen.
Implicit Button Style XAML
<Style TargetType="Button" x:Key="DefaultButtonStyle">
<Setter Property="Background" Value="{StaticResource NormalBrush}"/>
<Setter Property="Foreground" Value="{StaticResource TextContentBrush}"/>
<Setter Property="FontFamily" Value="{StaticResource ContentFontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource ContentFontSize}"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Padding" Value="10,3,10,3" />
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1" To="Pressed">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseIn" Exponent="-2"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="Pressed" GeneratedDuration="0:0:0.1" To="MouseOver">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseOut" Exponent="0"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="Normal" GeneratedDuration="0:0:0.01" To="MouseOver">
<VisualTransition.GeneratedEasingFunction>
<ExponentialEase EasingMode="EaseIn" Exponent="7"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
<VisualTransition From="MouseOver" GeneratedDuration="0:0:0.1" To="Normal">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource BaseColor2}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="PressedBorder" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1.0" KeySpline="0,0,0.0299999993294477,0.920000016689301"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0:0:0.01" Value="1.05"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter">
<EasingColorKeyFrame KeyTime="0" Value="{StaticResource BaseColor2}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.75"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.3"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused" >
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="BaseBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="1" >
<Border.Background>
<SolidColorBrush Color="{StaticResource BaseColor7}"/>
</Border.Background>
<Grid>
<Border x:Name="Background" BorderBrush="{StaticResource NormalInnerBorderBrush}" Background="{TemplateBinding Background}"/>
<Border x:Name="MouseOverBorder" Opacity="0" Background="{StaticResource MouseOverBrush}" Margin="-0.5,-0.5,0.5,0.5"/>
<Border x:Name="PressedBorder" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" Background="{StaticResource MouseOverBrush}" RenderTransformOrigin="0.5,0.5" Margin="-1">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</Grid>
</Border>
<Rectangle x:Name="DisabledVisualElement" Fill="{StaticResource DisabledBackgroundBrush}" IsHitTestVisible="false" Opacity="0" RadiusY="1" RadiusX="1" Stroke="{StaticResource DisabledBackgroundBrush}"/>
<ContentControl x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Foreground="{TemplateBinding Foreground}" Padding="2" />
<Rectangle x:Name="FocusVisualElement" Stroke="{StaticResource FocusedBrush}" Margin="-1" Opacity="0" /> <!--IsHitTestVisible="false"-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将模板中
ContentControl
的IsTabStop
属性设置为 False:Try setting the
IsTabStop
property of theContentControl
in your template to False:可能您的
Button
模板上的某些内容将IsTabStop
设置为 true。您的模板有多复杂?尝试重置为默认模板,看看是否能解决您的问题。
Probably there is something on your
Button
's template that hasIsTabStop
set to true.How complex is your template? Try resetting to the default template to see if that solves your problem.