【问题标题】:WPF TextBox border on Windows7Windows7上的WPF文本框边框
【发布时间】:2011-11-30 15:24:45
【问题描述】:

在 Windows7(也可能是 vista)中,TextBox 现在在接收输入焦点或鼠标悬停时会获得蓝色的强调矩形。

在我的 WPF 应用程序中,那个蓝色焦点矩形真的像拇指酸痛一样突出 - 它与应用程序其余部分的视觉风格不匹配。

如何禁用它 - 或者更好的是,自定义它?

(在 xaml 中将 FocusVisualStyle 设置为 {x:Null} 没有任何作用。)

【问题讨论】:

    标签: wpf xaml windows-7 textbox


    【解决方案1】:

    这是因为 TextBox 应用了默认样式。要修改此行为,您必须创建自定义样式/模板。看看这个样本

    <Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
            <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 x:Name="border" Background="#FF8F8F8F" BorderBrush="#FF585858" CornerRadius="3" BorderThickness="2">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
    
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="Silver"/>
                                </Trigger>
    
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="border" Value="0.15"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="Silver"/>
                            </Trigger>
    
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多