2014-10-30 89 views
0

enter image description here如何在可編輯的組合框中設置文本框的背景顏色?

顯示的圖像是我的wpf應用程序的一部分。我將它設置爲允許使用輸入的可編輯組合框。然而,TextBox裏面有一個白色的背景,我不喜歡很多。 ?我怎麼可能它的顏色改變爲不同的顏色(在這種情況下,我想將其更改爲"#FF2E2E2E"。)

我的XAML:

<ComboBox Height="23" Background="#FF2E2E2E" Foreground="#FF979797" Grid.Column="1" Margin="107,43,0,0" HorizontalAlignment="Left" Width="133" IsEditable="True" VerticalAlignment="Top" IsEnabled="False"> 
     <ComboBox.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF232323" /> 
     </ComboBox.Resources> 
    </ComboBox> 

UPDATE:

enter image description here

它與其他控件和資源無關。

+0

我還沒有找到一種方法從'ComboBox.Resources'中做到這一點,但如果你想創建一個完整的主題,只需找到這個鍵:''並將顏色更改爲所需的顏色。使用此網頁上的說明生成主題:http://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/ – Hosch250 2014-10-30 23:21:42

+0

您是否嘗試過自己的XAML代碼片段?我嘗試過使用*** Kaxaml ***,它看起來不像你的圖像顯示(內部TextBox沒有白色背景)。 – 2014-10-31 03:36:51

+0

@KingKing請參閱我的更新。 – HuStmpHrrr 2014-10-31 13:00:57

回答

2

好......終於找到了我自己。

,我們可以通過修改默認模板更改:

<SolidColorBrush x:Key="TextBox.Static.Background" Color="#FF2E2E2E"/> 
<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}"> 
         <Grid x:Name="templateRoot" SnapsToDevicePixels="true"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*"/> 
           <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/> 
          </Grid.ColumnDefinitions> 
          <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
           <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}"> 
            <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
             <ScrollViewer x:Name="DropDownScrollViewer"> 
              <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled"> 
               <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
                <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/> 
               </Canvas> 
               <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
              </Grid> 
             </ScrollViewer> 
            </Border> 
           </Themes:SystemDropShadowChrome> 
          </Popup> 
          <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/> 
          <Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}"> 
           <TextBox x:Name="PART_EditableTextBox" Background="{StaticResource TextBox.Static.Background}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          </Border> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Opacity" TargetName="border" Value="0.56"/> 
          </Trigger> 
          <Trigger Property="IsKeyboardFocusWithin" Value="true"> 
           <Setter Property="Foreground" Value="Black"/> 
          </Trigger> 
          <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true"> 
           <Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/> 
           <Setter Property="Color" TargetName="shadow" Value="#71000000"/> 
          </Trigger> 
          <Trigger Property="HasItems" Value="false"> 
           <Setter Property="Height" TargetName="dropDownBorder" Value="95"/> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsGrouping" Value="true"/> 
            <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
          </MultiTrigger> 
          <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false"> 
           <Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
           <Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 

我順便說一句刪除一些不相關的代碼。關鍵的一點是

<SolidColorBrush x:Key="TextBox.Static.Background" Color="#FF2E2E2E"/> 

     <Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}"> 

所有我需要做的就是改變邊框的顏色圍繞文本框下面的!

+0

真的很喜歡邊界設置綁定到厚度 – Florian 2016-12-13 14:40:49

0

我同意王中王:我做了一個新的WPF窗口,並添加大部分代碼的組合框(我沒有設置的IsEnabled = FALSE),它看起來像這樣對我說:
combobox

這是窗口的整個代碼:

<Window x:Class="WpfApplication35.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ComboBox Background="#FF2E2E2E" IsEditable="True" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100"> 
      <ComboBox.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF2E2E2E" /> 
      </ComboBox.Resources> 
     </ComboBox> 
    </Grid> 
</Window> 

也許你只需要對你的項目做一個「乾淨的」?

+0

請參閱我的更新。 – HuStmpHrrr 2014-10-31 12:59:47