如何从 WPF 中的文本框中删除边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31655853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:23:10  来源:igfitidea点击:

How to remove border from Textbox in WPF?

wpfxamltextbox

提问by F????? A???

enter image description here

在此处输入图片说明

These borders appear when they are clicked or hovered and don't go until the focus is lost. There are borders on all four sides but since it is embedded in a shorter grid, the top and bottom ones are not visible. How to remove these borders? Please provide an example if possible.

这些边框在被点击或悬停时出现,直到失去焦点才会消失。所有四个边都有边框,但由于它嵌入在较短的网格中,顶部和底部的不可见。如何去除这些边框?如果可能,请提供一个例子。

XAML:

XAML:

<Border x:Name="SearchBorder" BorderThickness="1" HorizontalAlignment="Left" Height="40" Margin="672,34,0,0" VerticalAlignment="Top" Width="355" Background="#3F000000">
        <Border.BorderBrush>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#4C000000" Offset="0"/>
                <GradientStop Color="#3FFFFFFF" Offset="1"/>
            </LinearGradientBrush>
        </Border.BorderBrush>
        <Grid>
            <TextBox x:Name="SearchBox" HorizontalAlignment="Left" Height="40" Width="296" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center"  SelectionBrush="Black" Background="#00000000" Foreground="#FF5B5B5B" FontSize="25" FontFamily="Segoe UI Light" BorderBrush="#00000000" CaretBrush="#FF6C6C6C"/>
            <TextBlock HorizontalAlignment="Left" Height="23" Margin="320,0,0,0" TextWrapping="Wrap" Text="&#xF002;" VerticalAlignment="Center" Width="21" FontFamily="FontAwesome" FontSize="25" Foreground="#FF919191"/>
            <Rectangle HorizontalAlignment="Left" Margin="311,-2,0,0" Width="1">
                <Rectangle.Stroke>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#3F404040" Offset="0"/>
                        <GradientStop Color="#3F686868" Offset="1"/>
                        <GradientStop Color="#59DADADA" Offset="0.502"/>
                    </LinearGradientBrush>
                </Rectangle.Stroke>
            </Rectangle>
        </Grid>
    </Border>

回答by almulo

Try BorderThickness="0"

尝试 BorderThickness="0"

<TextBox x:Name="SearchBox" BorderThickness="0" HorizontalAlignment="Left" Height="40" Width="296" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center"  SelectionBrush="Black" Background="#00000000" Foreground="#FF5B5B5B" FontSize="25" FontFamily="Segoe UI Light" BorderBrush="#00000000" CaretBrush="#FF6C6C6C"/>

回答by Merry

you can remove the border by just setting the BorderBrush property to Transparent.

您只需将 BorderBrush 属性设置为 Transparent 即可移除边框。

回答by Chandraprakash

Normal View:Set the BorderBrush property to "Transparent"

普通视图:将 BorderBrush 属性设置为“透明”

<Button Name="BtnTest"
        Content="Test"
        BorderBrush="Transparent"
        Height="20"
        Width="75"/>

Hovering View:While you are hovering the control with mouse, Include these Control Template for Button or any control like Textbox or Textblock etc.. under the Window.Resources Tag

悬停视图:当您用鼠标悬停控件时,在 Window.Resources 标签下包含按钮或任何控件(如文本框或文本块等)的这些控件模板

<Window.Resources>
           <Style TargetType="Button">

                <!--Default Values-->
                <Setter Property="BorderBrush"
                        Value="Transparent"/>

                <!--Transparent Highlight-->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" 
                                                  VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

            </Style>
</Window.Resources>