焦点图像在文本框上第一次不起作用 [英] Focus image does not work for first time on Textbox
问题描述
< Style x:Key =TextBoxFocusVisualStyle>
< Setter Property =Control.Template>
< Setter.Value>
< ControlTemplate>
< / ControlTemplate>
< / Setter>
< / style>
在窗口xaml文件中,我使用了下面这个样式
< TextBox Grid.Column =1Height =34Margin =186,48,0,0Name =txtEmailIdVerticalAlignment =Top KeyboardNavigation.TabIndex =0MaxWidth =293HorizontalAlignment =LeftWidth =293Text =FocusVisualStyle ={DynamicResource TextBoxFocusVisualStyle}/>
但问题在于它在加载时不起作用。当窗口加载,然后最初的重点放在该文本框,当时它不显示图像。但是,当我导航到其他文本框(和其他控制),然后它显示焦点图像。最后当我焦点返回到该文本框然后显示焦点图像
所以问题是,它不显示焦点图像第一次在窗口加载时。请注意,我错了。
解决方案
考虑到FocusVisualStyle仅适用于通过键盘(TAB键)进行聚焦的控件。
这与您获得的逻辑焦点不同,例如使用
Control.SetFocus()
有关Focus的概述,请参阅
http://msdn.microsoft.com对于你的问题可能的解决方法是使用DependencyProperty IsFocused使用Style而不是FocusVisualStyle
< Style x:Key =TextBoxStyleTargetType ={x:Type Control}>
< Style.Triggers>
<触发属性=IsFocused值=真>
< Setter Property =Control.Template>
< Setter.Value>
< ControlTemplate>
< Image Stretch =FillMargin = - 8,-6,-8,-6Source =/ WPFApp; component / Resources / txtFocus.png/>
< / ControlTemplate>
< / Setter>
< / Trigger>
< / style>
然后在主窗口中
< TextBox Grid.Column =1Height =34Margin =186,48,0,0Name =txtEmailId
VerticalAlignment =TopKeyboardNavigation .TabIndex =0MaxWidth =293
HorizontalAlignment =LeftWidth =293Text =
Style ={DynamicResource TextBoxFocusVisualStyle}Background =White/>
希望这个heps
i want to show a focus image around a text box when it got focus. so i create following style
<Style x:Key="TextBoxFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Image Source="/WPFApp;component/Resources/txtFocus.png" Stretch="Fill" Margin="-8,-6,-8,-6"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and in window xaml file i used this style as following
<TextBox Grid.Column="1" Height="34" Margin="186,48,0,0" Name="txtEmailId" VerticalAlignment="Top" KeyboardNavigation.TabIndex="0" MaxWidth="293" HorizontalAlignment="Left" Width="293" Text="" FocusVisualStyle="{DynamicResource TextBoxFocusVisualStyle}"/>
but problem is that it does not work during loading. When window load then initially focus is on that textbox and at that time it does not show the image .However when i navigate to other textbox (and other control) then it show focus image. and finally when i focus return to that textbox then it display the focus image
so problem is that it does not show focus image first time on when window loaded. Please suggest that where i am wrong.
Consider that FocusVisualStyle applies to a control only when focused by keyboard (TAB key).
This is different from the logical focus that you obtain for example using
Control.SetFocus()
For an overview on Focus have a look at
http://msdn.microsoft.com/en-us/library/aa969768.aspx
A possible workaround for your problem is work with DependencyProperty IsFocused an use Style instead of FocusVisualStyle
<Style x:Key="TextBoxStyle" TargetType="{x:Type Control}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Image Stretch="Fill" Margin="-8,-6,-8,-6" Source="/WPFApp;component/Resources/txtFocus.png" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
And then in the main Window
<TextBox Grid.Column="1" Height="34" Margin="186,48,0,0" Name="txtEmailId"
VerticalAlignment="Top" KeyboardNavigation.TabIndex="0" MaxWidth="293"
HorizontalAlignment="Left" Width="293" Text=""
Style="{DynamicResource TextBoxFocusVisualStyle}" Background="White" />
Hope this heps
这篇关于焦点图像在文本框上第一次不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!