首页 文章

使用模板时TextBox文本高亮/光标不可见

提问于
浏览 1723
0

我一直在用Styles更改我的应用程序的界面,使用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吗?

1 回答

  • 0

    这主要是因为内容主持人 . 它应该是一个滚动查看器,它是内容主机的一部分 . 这是你的代码与工作添加 .

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

    EDIT: 这是根据您从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>
    

相关问题