2017-07-09 125 views
0

我一直在尋找小時,試圖找出如何更改組合框項目的高亮顏色(NOT MouseOver)。我有一個可編輯的組合框,用於搜索綁定到它的列表中的元素。在列表中鍵入元素名稱將突出顯示組合框下拉列表中的元素。但該元素突出了一個默認的「淺灰色」,這是很難看到...更改組合框項目的高亮顏色WPF

enter image description here

我試圖重寫這篇文章中所描述的默認高亮色彩:Set ComboBox selected item highlight color,沒有運氣。

再次發生這種情況不是鼠標懸停在盒子中搜索或使用鍵盤上的向上/向下箭頭鍵導航列表。我也考慮過可能在查看鍵盤焦點屬性,但不知道如何在模板中實現它。讓我知道,如果需要,我可以發佈模板。

我剛剛爲這個組合框啓動了一個全新的模板,所以它應該都很乾淨。我只是不能確定我應該瞄準什麼元素/觸發器/邊界。

有人對此有所瞭解嗎?

回答

0

找到這個問題的答案將關閉此文章在這裏的工作:Set style on ComboBoxItem from template for ComboBox

我申請修改ComboBoxItem模板,我的「編輯ComboBox」的模板。您可以在下面看到這裏,我在IsEditable風格觸發增加了一個「ItemContainerStyle」屬性:

<Style x:Key="CmbRoundedAutoComplete" TargetType="{x:Type ComboBox}"> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource ComboBox.Static.Border}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="Padding" Value="6,3,5,3"/> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="Both"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/> 
     <Style.Triggers> 
      <Trigger Property="IsEditable" Value="true"> 
       <Setter Property="IsTabStop" Value="false"/> 
       <Setter Property="Padding" Value="2"/> 
       <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/> 
       <!-- Added this line here --> 
       <Setter Property="ItemContainerStyle" Value="{DynamicResource ComboBoxItemShieldStyle}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

有一個MultiTrigger位於自定義「ItemContainerStyle」控件模板(重要的是要注意的一部分,這是樣式不是實際的ControlTemplate。)。 ControlTemplate元素駐留在樣式中。

您可以通過手動將ComboBoxItem添加到XAML中現有的ComboBox並生成一個模板來生成一個「ComboboxItem」模板。

<MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="IsSelected" Value="True"/> 
       <Condition Property="IsMouseOver" Value="False"/> 
       <Condition Property="IsKeyboardFocused" Value="False"/> 
      </MultiTrigger.Conditions> 
     <!-- ANSWER IS HERE Change color on the 2 below lines for border & background --> 
       <Setter Property="Background" TargetName="Bd" Value="#D1E8FF"/> 
       <Setter Property="BorderBrush" TargetName="Bd" Value="#66A7E8"/> 
     </MultiTrigger> 

你可以在上面的代碼中看到我曾經評論說:「答案就在這裏」,您可以更改已通過在組合框中場無論是打字選擇或導航到它與該項目的背景顏色箭頭鍵。

在「結果」(以紅色文字顯示)中,當前項目的「背景」是相當藍色的顏色,而不是上述原始問題中的蹩腳灰色...

最終結果:

enter image description here

+0

我們可以有一個解決方案來突出顯示可能項目的一部分,例如:當我輸入「辦公室」時,有3名候選人(見上述圖片),3名候選人的紅色文字「辦公室」是紅色的? –