焦点图像在Textbox上第一次不起作用

时间:2022-06-23 00:59:01

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

并在窗口xaml文件中我使用此样式如下

<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.

所以问题是它在窗口加载时第一次没有显示焦点图像。请建议我错在哪里。

1 个解决方案

#1


0  

Consider that FocusVisualStyle applies to a control only when focused by keyboard (TAB key).

考虑到FocusVisualStyle仅在通过键盘(TAB键)聚焦时才应用于控件。

This is different from the logical focus that you obtain for example using

这与您使用的逻辑焦点不同

 Control.SetFocus()

For an overview on Focus have a look at

有关Focus的概述,请查看

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

您的问题的一个可能的解决方法是使用DependencyProperty IsFocused使用Style而不是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

希望这会好转

#1


0  

Consider that FocusVisualStyle applies to a control only when focused by keyboard (TAB key).

考虑到FocusVisualStyle仅在通过键盘(TAB键)聚焦时才应用于控件。

This is different from the logical focus that you obtain for example using

这与您使用的逻辑焦点不同

 Control.SetFocus()

For an overview on Focus have a look at

有关Focus的概述,请查看

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

您的问题的一个可能的解决方法是使用DependencyProperty IsFocused使用Style而不是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

希望这会好转