0

当我用鼠标单击文本框时,不会显示焦点。当我通过键盘进入文本框时,会显示焦点。我正在尝试下面的代码。所以请任何人都可以建议我如何解决这个问题。

 <Style x:Key="TextBoxFocusVisualStyle" >
     <Setter Property="Control.Template">
          <Setter.Value>
               <ControlTemplate>
                        <Image Source="/Mit;component/Resources/txtFocus.png"  Stretch="Fill"  Margin="-8,-6,-8,-6"/>
               </ControlTemplate>
          </Setter.Value>
     </Setter>
 </Style>

<TextBox  Grid.Column="2" Height="44" Margin="20,48,0,0" Name="txtEmailId" VerticalAlignment="Top" KeyboardNavigation.TabIndex="2" MaxWidth="400" HorizontalAlignment="Left" Width="350" Text=""  FocusVisualStyle="{DynamicResource TextBoxFocusVisualStyle}" VerticalContentAlignment="Center" FontWeight="SemiBold" FontSize="18" ContextMenu="{x:Null}" />

在@hattenn 回答之后,我还尝试了以下代码:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>



        <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <VisualBrush>
                                <VisualBrush.Visual>
                                    <Grid>
                                        <Image Source="/WpfApplication1;component/Resources/txtFocus.png"  Stretch="Fill"  Margin="-8,-6,-8,-6"/>
                                    </Grid>
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>


    </Application.Resources>

<TextBox Height="23" Name="textBox1" Width="120" Focusable="True" FocusVisualStyle="{DynamicResource TextBoxStyle}"/>

但这对我不起作用。没啥事儿。请建议。谢谢

4

2 回答 2

1

FocusVisualStyle仅用于键盘焦点,您可以在此处查看:

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.focusvisualstyle.aspx

对于一般焦点,您可以使用该IsFocused属性,更多信息如下:

http://msdn.microsoft.com/en-us/library/system.windows.uielement.isfocused.aspx

例如,您可以尝试这样的事情:

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush>
                        <VisualBrush.Visual>
                            <Grid>
                                <Image Source="/Mit;component/Resources/txtFocus.png"  Stretch="Fill"  Margin="-8,-6,-8,-6"/>
                            </Grid>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>
于 2013-05-21T11:39:19.177 回答
0

只需将样式添加到文本框:

position:relative;
于 2014-08-13T11:54:53.203 回答