WPF SystemColors:文本框边框的颜色

发布于 2024-07-24 05:24:26 字数 827 浏览 3 评论 0原文

我正在尝试创建一个带有嵌入式放大镜图标的搜索文本框。 到目前为止,我有以下标记:

<Border DockPanel.Dock="Bottom" Margin="2,4,0,4" 
        BorderThickness="1" SnapsToDevicePixels="True" 
        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
    <DockPanel>
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
            <Image Source="/Resources/search-13x13.png" Width="13"/>
        </StackPanel>
        <TextBox Name="searchTextBox" DockPanel.Dock="Bottom" BorderThickness="0" 
                 Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"/>
    </DockPanel>
</Border>

但是,我在 SystemColors 中找不到该条目,该条目将为我提供与标准 TextBox 边框相同的颜色。 默认情况下,这是一种蓝色。 难道我真的很傻吗?!?

编辑:顺便说一句,图像包含在堆栈面板中,因为我也计划在其中放置一个下拉箭头。

I am trying to make a search TextBox with an embedded magnifying glass icon. I have the following markup so far:

<Border DockPanel.Dock="Bottom" Margin="2,4,0,4" 
        BorderThickness="1" SnapsToDevicePixels="True" 
        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
    <DockPanel>
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
            <Image Source="/Resources/search-13x13.png" Width="13"/>
        </StackPanel>
        <TextBox Name="searchTextBox" DockPanel.Dock="Bottom" BorderThickness="0" 
                 Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"/>
    </DockPanel>
</Border>

However, I can't find the entry in SystemColors which will give me the same color as the standard TextBox border. This is a blueish color by default. Am I being really stupid here?!?

EDIT: btw, the image is contained in a stackpanel because I'm planning to put a dropdown arrow in there as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦初启 2024-07-31 05:24:26

您可以尝试使用 Microsoft.Windows.Themes.ListBoxChrome 而不是边框​​; 这就是 TextBox 的默认模板使用的内容:

<ControlTemplate TargetType="TextBoxBase" 
                 xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
    <mwt:ListBoxChrome Name="Bd" SnapsToDevicePixels="True">
        <ScrollViewer Name="PART_ContentHost" 
                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
    </mwt:ListBoxChrome>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter TargetName="Bd" Property="Panel.Background" 
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
            <Setter Property="TextElement.Foreground" 
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

您应该能够仅使用 ListBoxChrome 而不是 Border,而不是重新模板化 TextBox 以匹配您提供的代码。

You might try using Microsoft.Windows.Themes.ListBoxChrome instead of the Border; that's what the default template for TextBox uses:

<ControlTemplate TargetType="TextBoxBase" 
                 xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
    <mwt:ListBoxChrome Name="Bd" SnapsToDevicePixels="True">
        <ScrollViewer Name="PART_ContentHost" 
                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
    </mwt:ListBoxChrome>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter TargetName="Bd" Property="Panel.Background" 
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
            <Setter Property="TextElement.Foreground" 
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

You should be able to use just ListBoxChrome instead of Border rather than re-templating TextBox to match the code you presented.

在梵高的星空下 2024-07-31 05:24:26

我能够通过以下方式以编程方式获取它:

TextBox.BorderBrush = SystemColors.ControlDarkBrush;

I was able to get it programatically with:

TextBox.BorderBrush = SystemColors.ControlDarkBrush;
握住你手 2024-07-31 05:24:26

根据尼古拉斯·阿姆斯特朗的回答,该解决方案对我有用:

<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomTextBox}">
                <mwt:ListBoxChrome x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" RenderMouseOver="{TemplateBinding IsMouseOver}">
                        <ScrollViewer x:Name="PART_ContentHost" />
                </mwt:ListBoxChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Based on Nicholas Armstrong's answer, that solution is working for me:

<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomTextBox}">
                <mwt:ListBoxChrome x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" RenderMouseOver="{TemplateBinding IsMouseOver}">
                        <ScrollViewer x:Name="PART_ContentHost" />
                </mwt:ListBoxChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
归属感 2024-07-31 05:24:26

对于任何正在寻找画笔列表及其颜色在不同主题/操作系统下的外观的人:
https: //i.sstatic.net/E2VBv.png

最初发布:http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx

To anyone that is looking for a list of Brushes and what their colors will look like with different themes/OS:
https://i.sstatic.net/E2VBv.png

Originally posted: http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx.

森林散布 2024-07-31 05:24:26

这看起来很黑客,但我通过创建一个文本框(可能已折叠)并绑定到其边框画笔而获得了最好的运气。

It seems hackish, but I've had the best luck by creating a textbox (perhaps collapsed) and binding to its border brush.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文