WPF SystemColors: color of TextBox border

前端 未结 5 941

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



        
相关标签:
5条回答
  • 2020-12-17 00:50

    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.

    0 讨论(0)
  • 2020-12-17 00:54

    To anyone that is looking for a list of Brushes and what their colors will look like with different themes/OS

    I would look here: http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx

    0 讨论(0)
  • 2020-12-17 01:00

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

    0 讨论(0)
  • 2020-12-17 01:09

    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>
    
    0 讨论(0)
  • 2020-12-17 01:10

    I was able to get it programatically with:

    TextBox.BorderBrush = SystemColors.ControlDarkBrush;
    
    0 讨论(0)
提交回复
热议问题