使用模板时,不显示TextBox文本突出显示/光标

时间:2018-12-10 09:17:29

标签: .net wpf

到目前为止,我一直在使用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 :(得分: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>

编辑:根据您的下一个问题,这是另一个示例,它是从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>