文本框与选项卡聚焦时,光标不显示

时间:2019-03-19 02:06:06

标签: wpf textbox

使用 Tab 聚焦TextBox时,光标不显示。键入时不会改变。仅当用户在文本框内单击时,此问题才能解决。

这是我的文本框的代码:

<TextBox x:Name="NameField"
             Style="{StaticResource placeHolder}"
             Tag="Name" 
             FontFamily="Courier New"
             FontSize="48"
             VerticalAlignment="Top"
             FontWeight="Bold"
             BorderBrush="{x:Null}"
             SelectionBrush="{DynamicResource AccentColor}"
             BorderThickness="0"
             TextChanged="ChangeName"
             Height="49"
             KeyDown="UndoRedoKeyPress" />

我尝试通过将this.Cursor = Cursors.IBeam;添加到“更改文本事件”来解决该错误,但此操作不起作用。

编辑:我在一个新项目中用两个样式在一个空窗口中复制了此故障:

<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                             x:Name="textSource" 
                             Background="Transparent"
                             Foreground="{DynamicResource ForegroundColor}"
                             CaretBrush="{DynamicResource ForegroundColor}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             SelectionBrush="{TemplateBinding SelectionBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Panel.ZIndex="2"
                             AcceptsReturn="{TemplateBinding AcceptsReturn}"
                             AcceptsTab="{TemplateBinding AcceptsTab}"
                             TextWrapping="{TemplateBinding TextWrapping}" />
                        <TextBox Text="{TemplateBinding Tag}" Background="{DynamicResource BackgroundColor}" Panel.ZIndex="1" BorderBrush="{TemplateBinding BorderBrush}"
                             SelectionBrush="{TemplateBinding SelectionBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <Setter Property="Foreground" Value="Transparent"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                            <Setter Property="Foreground" Value="LightGray"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

单击第一个文本框,然后按 Tab 突出显示第二个文本框。键入时光标将不可见。

1 个答案:

答案 0 :(得分:1)

TextBox样式应包含任何子TextBox元素。试试这个:

<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Foreground" Value="{DynamicResource ForegroundColor}" />
    <Setter Property="CaretBrush" Value="{DynamicResource ForegroundColor}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="border"
                                Background="Transparent"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Panel.ZIndex="2"
                                SnapsToDevicePixels="True">
                    <Grid>
                        <TextBlock Text="{TemplateBinding Tag}" Foreground="LightGray">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" 
                                          HorizontalScrollBarVisibility="Hidden" 
                                          VerticalScrollBarVisibility="Hidden"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>