wpf 文本框平面边框样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2475086/
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
wpf textbox flat border style
提问by Muhammad Adnan
need to have flat border style for wpf based textbox... really surprised to see there is no easy way to get this like was in winforms textbox BorderStyle.FixedSingle
需要为基于 wpf 的文本框设置扁平边框样式...真的很惊讶地发现没有简单的方法可以像在 winforms 文本框 BorderStyle.FixedSingle
is there any easy way to get this done for wpf textbox
有没有什么简单的方法可以为 wpf 文本框完成这项工作
回答by Michael Shaw
The way to do this is to use a control template to draw the border yourself. You can do this in many different ways, heres a couple for you to look at.
这样做的方法是使用控件模板自己绘制边框。您可以通过多种不同的方式来做到这一点,这里有一些供您查看。
The quick hack approach:
快速破解方法:
<TextBox>
<TextBox.Template>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Rectangle Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="1"/>
<TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0"/>
</Grid>
</ControlTemplate>
</TextBox.Template>
</TextBox>
and then theres using resources...
然后是使用资源...
<ResourceDictionary>
<Color x:Key="detailMark">#FFA1A9B3</Color>
<SolidColorBrush x:Key="detailMarkBrush" Color="{StaticResource ResourceKey=detailMark}" />
<Style x:Key="flatTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Rectangle Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="1"/>
<TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
and then you can use the style:
然后你可以使用样式:
<TextBox Style="{StaticResource ResourceKey=flatTextBox}" />
回答by Kishore Kumar
<TextBox BorderThickness="1" BorderBrush="Black">
just try this by black or gray
试试这个黑色或灰色
回答by Val
This is better way to me, make a custom template with border, to override the default one.
这对我来说是更好的方法,制作带有边框的自定义模板,以覆盖默认模板。
And most important make ScrollViewer
named PART_ContentHost
, to fit inner TemplatePart
, and for any other features work like default.
最重要的是 make ScrollViewer
named PART_ContentHost
,适合内部TemplatePart
,以及任何其他功能都像默认一样工作。
simular to example from MSDN:
类似于MSDN 中的示例:
<Style TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border CornerRadius="2" Padding="2" Background="#19212F" BorderBrush="Red" BorderThickness="1">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>