2009-06-05 159 views
6

我正在尝试使用嵌入式放大镜图标进行搜索TextBox。我有以下标记至今:WPF SystemColors:TextBox边框的颜色

<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中的条目,这将给我的颜色作为标准文本框边框相同。这是默认的蓝色。我在这里真的很蠢吗?!?

编辑:顺便说一句,图像包含在一个堆栈面板,因为我打算在那里放下一个下拉箭头。

+0

我不认为你是愚蠢的 - 我已经有同样的问题,试图找到列表框的边框颜色(我认为相同的颜色)。我不确定它在任何地方浮出水面。 – 2009-06-05 12:33:47

+0

你能找到你想要的颜色并获得它的RGB值的例子吗?这可能有助于确定它是哪种颜色。 – ChrisF 2009-06-05 12:35:15

+0

最好选择文本框边框的颜色值,并将其用作边界刷 – 2009-06-05 12:38:28

回答

4

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

<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代替边界,而不是重新模板文本框来匹配你提供的代码。

2

我能够用得到它编程:

TextBox.BorderBrush = SystemColors.ControlDarkBrush; 
1

看来的hackish,但我已经通过创建一个文本框(也许崩溃),并结合其边框刷过的最好的运气。

3

基于尼古拉斯·阿姆斯特朗的答案,该解决方案是工作对我来说:

<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>