5
我注意到了TextBox的一個奇怪行爲,而BorderThickness
屬性設置爲1 - 焦點導致邊框改變顏色(像白色一樣)。 但是,如果我將邊框厚度設置爲不同於1的值,例如.99或1.01,則問題會消失。WPF中的文本框的邊框厚度(一個bug?)
它是WPF中的錯誤嗎?還是它的目的?
我注意到了TextBox的一個奇怪行爲,而BorderThickness
屬性設置爲1 - 焦點導致邊框改變顏色(像白色一樣)。 但是,如果我將邊框厚度設置爲不同於1的值,例如.99或1.01,則問題會消失。WPF中的文本框的邊框厚度(一個bug?)
它是WPF中的錯誤嗎?還是它的目的?
這是TextBoxes的Aero樣式的默認行爲。要禁用它,你需要重新設置TextBox。您可以從here中獲取默認樣式(請參閱下載示例)。
在TextBoxBase(TextBox所基於的)的默認樣式中,您會看到它使用ListBoxChrome。此元素在Presentation.Aero程序集中定義,並負責呈現「聚焦」外觀。您可以簡單地刪除RenderFocus設置,也可以刪除RenderMouseOver,或將其替換爲邊框。
然後你想要將它包含在你的應用程序資源中。
<LinearGradientBrush x:Key="TextBoxBorder"
StartPoint="0,0" EndPoint="0,20" MappingMode="Absolute">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#ABADB3" Offset="0.05" />
<GradientStop Color="#E2E3EA" Offset="0.07" />
<GradientStop Color="#E3E9EF" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<Style x:Key="{x:Type TextBoxBase}" TargetType="{x:Type TextBoxBase}" BasedOn="{x:Null}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="1" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border >
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBoxBase}}" TargetType="{x:Type TextBox}"/>
如果你看看在反射器ListBoxChrome類(特別是法的OnRender),你可以看到它只會呈現專注的神情,如果它是了borderThickness,「1,1,1,1」。
什麼是您的視頻適配器的DPI設置爲? SnapToDevicePixels有效嗎? – 2010-11-08 21:36:54
我想我可以重現這一點。如果你有一個文本框並且它獲得焦點,那麼邊框會改變顏色。但是,如果BorderThickness不是「1」,則不會發生。從未見過這個,不要以爲我以前曾經設置過TextBox的BorderThickness。 (+1) – 2010-11-08 21:49:22
@Hans,我認爲SnapToDevicePixels沒有任何合理的效果。只有當TextBox獲得關鍵焦點時(即正在寫入並且鼠標移動到TextBox上),問題纔會發生。 – Jamie 2010-11-08 21:59:51