首页 > 解决方案 > 使用模板时文本框文本突出显示/光标不可见

问题描述

我一直在用样式改变我的应用程序的界面,使用 ControlTemplates,到目前为止,对于滑块、按钮和标签,一切都很好。

但我无法让 TextBox 的文本显示任何可编辑的迹象。它可以编辑,但没有可见的光标或选择突出显示。

这是我的模板代码:

<Style TargetType="TextBox">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="TextBox">
            <Border CornerRadius="2" BorderThickness="1" Background="{StaticResource Dark}" BorderBrush="{StaticResource Medium}" Margin="2">
                <ContentPresenter Content="{TemplateBinding Text}" x:Name="tbText" TextBlock.Foreground="{StaticResource Light}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

在这种情况下,如何使文本选择/光标可见?我是否使用了错误的 ContentPresenter?

标签: .netwpf

解决方案


这主要是因为内容演示者。它应该是一个滚动查看器,它是内容主机的一部分。这是您的代码与工作添加。

        <Style TargetType="TextBox">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Border x:Name="border" CornerRadius="2" BorderThickness="1" Background="White" BorderBrush="Gray" Margin="2">
                        <ScrollViewer
                            x:Name="PART_ContentHost"
                            Focusable="false"
                            HorizontalScrollBarVisibility="Hidden"
                            VerticalScrollBarVisibility="Hidden" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

编辑:这是另一个示例,根据您从 Blender 获取的默认覆盖文本框样式的下一个问题。在这里,您可以看到如何设置每个属性以自定义所有内容。

    <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3" />
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA" />
    <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5" />

    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
        <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="AllowDrop" Value="true" />
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border
                        x:Name="border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True">
                        <ScrollViewer
                            x:Name="PART_ContentHost"
                            Focusable="false"
                            HorizontalScrollBarVisibility="Hidden"
                            VerticalScrollBarVisibility="Hidden" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="border" Property="Opacity" Value="0.56" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource TextBox.MouseOver.Border}" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource TextBox.Focus.Border}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true" />
                    <Condition Property="IsSelectionActive" Value="false" />
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>

推荐阅读