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
How to Create Textbox with Dashed Border or Double Line Border
提问by dongx
采纳答案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>


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>



