wpf 如何创建带有虚线边框或双线边框的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13595349/
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-13 06:23:23  来源:igfitidea点击:

How to Create Textbox with Dashed Border or Double Line Border

c#wpfxaml

提问by dongx

How to create the following two textboxes (NOT rectangles) in XAML or C# code

如何在 XAML 或 C# 代码中创建以下两个文本框(不是矩形)

Textboxes with border

带边框的文本框

采纳答案by D J

You should use Blend to edit the control template for text box. I did it for double boder for you.

您应该使用 Blend 来编辑文本框的控件模板。我这样做是为了给你双重约束。

 <Style x:Key="DashedTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border BorderBrush="Black" BorderThickness=".5">
                         <Border BorderBrush="Black" Margin="1" BorderThickness=".5">
                                <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </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>

and apply it on text box like.

并将其应用于文本框之类的。

<TextBox Text="Hello world" Width="100" Height="30.667" Canvas.Left="150" Canvas.Top="90" Style="{DynamicResource DashedTextBoxStyle}"/>

You may need to customize it for more better look. Also I have removed default border from style.

您可能需要对其进行自定义以获得更好的外观。我也从样式中删除了默认边框。

You can do same for your dashed one.

你可以对你的虚线做同样的事情。

Hope it helps.

希望能帮助到你。

回答by sa_ddam213

You could use rectange to create the "Dash" style

您可以使用 rectange 来创建“Dash”样式

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Width="200" Height="40" Name="Textblock1" Text="Hello World!" TextAlignment="Center" FontSize="20"/>
        <Rectangle Width="{Binding ElementName=Textblock1, Path=ActualWidth}" Height="{Binding ElementName=Textblock1, Path=ActualHeight}" StrokeDashArray="0.0 6.0 0.0" Stroke="Black" StrokeThickness="2"  />
    </Grid>
</Window>

enter image description here

在此处输入图片说明

And for double line you could possibly create 2 borders

对于双线,您可能会创建 2 个边框

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border BorderBrush="Black" Width="200" Height="40" BorderThickness="1">
            <Border BorderBrush="Black" Margin="2" BorderThickness="1">
                <TextBlock  Name="Textblock1" Text="Hello World!" TextAlignment="Center" FontSize="20"/>
            </Border>
        </Border>
    </Grid>
</Window>

enter image description here

在此处输入图片说明