wpf 文本框样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17392189/
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
Text box styling
提问by Andrei Diaconu
Whenever I try to apply a style to a textbox, it becomes unresponsive to user input. Can you tell me a way to solve this?Here is the xaml code I am using:
每当我尝试将样式应用于文本框时,它都会对用户输入无响应。你能告诉我解决这个问题的方法吗?这是我正在使用的 xaml 代码:
<Style x:Key="textbox"
TargetType="TextBox">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border BorderThickness="3"
Background="{TemplateBinding Background}"
Name="border">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="border"
Property="BorderBrush"
Value="#9E5971" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
回答by Athari
Replace
代替
<ContentPresenter
HorizontalAlignment="Center" VerticalAlignment="Center" />
with
和
<ScrollViewer
x:Name="PART_ContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
ScrollViewernamed PART_ContentHostis a mandatory part of any TextBoxcontrol template. You should use Default WPF Control Styles and Templatesas reference instead of coming up with your own template.
ScrollViewernamedPART_ContentHost是任何TextBox控制模板的必需部分。您应该使用默认 WPF 控件样式和模板作为参考,而不是想出您自己的模板。

