2009-06-05 155 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>