wpf - 创建使用 Windows 主题的边框

标签 wpf xaml border

我想在 WPF (XAML) 中创建一个边框,该边框使用当前 Windows 主题(XP、Areo、Classic、Modern UI)中的颜色。

我已经尝试使用 SystemColors 类中的一些画笔,但边框看起来与 TextBox 的默认边框不同。

有没有办法获得真实的边框笔刷?

最佳答案

下面给出的是 WPF 对于 TextBox 控件使用的默认样式:

  <Style TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
    <Setter Property="OverridesDefaultStyle"
            Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation"
            Value="None" />
    <Setter Property="FocusVisualStyle"
            Value="{x:Null}" />
    <Setter Property="MinWidth"
            Value="120" />
    <Setter Property="MinHeight"
            Value="20" />
    <Setter Property="AllowDrop"
            Value="true" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBoxBase}">
          <Border Name="Border"
                  CornerRadius="2"
                  Padding="2"
                  BorderThickness="1">
            <Border.Background>
              <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
            </Border.Background>
            <Border.BorderBrush>
              <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
            </Border.BorderBrush>
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Disabled">
                  <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                  Storyboard.TargetProperty="(Panel.Background).
                      (SolidColorBrush.Color)">
                      <EasingColorKeyFrame KeyTime="0"
                                           Value="{StaticResource DisabledControlLightColor}" />
                    </ColorAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="ReadOnly">
                  <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                  Storyboard.TargetProperty="(Panel.Background).
                      (SolidColorBrush.Color)">
                      <EasingColorKeyFrame KeyTime="0"
                                           Value="{StaticResource DisabledControlDarkColor}" />
                    </ColorAnimationUsingKeyFrames>
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="MouseOver" />
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ScrollViewer Margin="0"
                          x:Name="PART_ContentHost" />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

关于wpf - 创建使用 Windows 主题的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15298820/

相关文章:

wpf - 打印带有页眉和页脚的 RTF - FlowDocument 或其他什么?

c# - 无效操作异常 : This freezable cannot be frozen

c# - ScintillaNET 自动完成列表问题

WPF UserControl 或 ControlTemplate...(不确定)

wpf - 使用触发器更改 XAML 映射图钉的背景颜色

html - 使用图像制作边框 CSS3

c# - WPF - 本地/云应用程序数据存储

c# - 设计具有可扩展 TextBlock 的 WPF 控件

html - 无序列表中的边框不适合 div 的整个宽度

css - 使边框环绕整个窗口会导致显示垂直滚动条吗?