WPF TextBox在焦点时具有蓝色轮廓

时间:2017-10-21 18:44:31

标签: wpf xaml border

所以我正在研究WPF中的一个程序,我做了一个没有边框的文本框,当你没有焦点时,(应该)在聚焦时有一条黑线。但是,该线显示为蓝色。我环顾了谷歌,似乎什么都没有用。

<Window x:Class="RequestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:RequestApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="900.156" Width="1414.292">
<Window.Resources>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="BorderBrush" Value="Black" />
                <Setter Property="Text" Value="Come Jam wit me man" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="7*"/>
        <RowDefinition Height="18*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1067*"/>
        <ColumnDefinition Width="433*"/>
    </Grid.ColumnDefinitions>
    <TextBox x:Name="Request" HorizontalAlignment="Left" Margin="37,130,0,0" BorderThickness="0" Text="Request" VerticalAlignment="Top" RenderTransformOrigin="-2.971,0.656" Height="39" Width="594" FontFamily="Modern Sans" FontSize="25" InputScope="Default" GotFocus="Request_GotFocus" LostFocus="Request_LostFocus" SelectionBrush="{x:Null}" BorderBrush="Black" Foreground="Gray"/>
</Grid>

PS:我不知道关于XAML的第一件事

1 个答案:

答案 0 :(得分:1)

以下是TextBox的默认样式。您正在战斗的行为是由这两个ControlTemplate触发器引起的 - IsMouseOver和IsKeyboardFocused。只需编辑纯色画笔定义即可:

<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" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
            </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="{StaticResource TextBox.MouseOver.Border}"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" 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>