【问题标题】:WPF TextBox has Blue Outline while in focusWPF 文本框在焦点上具有蓝色轮廓
【发布时间】:2021-11-15 16:13:33
【问题描述】:

所以我正在开发 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 的第一件事

【问题讨论】:

    标签: wpf xaml border


    【解决方案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>
    

    【讨论】:

    • 好吧,我认为我修改它做得很好,虽然我可能犯了一些错误。你能解释一下为什么会这样吗?谢谢!
    【解决方案2】:

    我在寻找一种方法来消除 WPF 文本框中显示为蓝色且不适合我的设计的边框后发现了这个问题。

    我通过将 Border-Thickness 设置为“0”来做到这一点:

    <TextBox x:Name="textBoxSearch" BorderThickness="0"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2022-09-29
      • 1970-01-01
      • 2016-08-04
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多