2012-10-03 39 views
9

根據this,覆蓋ControlBrushKey資源應該改變列表框選定項目的背景顏色,當它沒有焦點時。我創建了一個簡單的例子來反駁這一點:重寫ListBoxItem背景顏色時不焦點(.NET 4.5)

<StackPanel> 
    <ListBox> 
     <ListBox.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/> 
     <!--SelectedItem without focus but doesn't really work--> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Orange" /> 
     </ListBox.Resources> 
     <ListBoxItem> 
     Item 1 
     </ListBoxItem> 
     <ListBoxItem> 
     Item 2 
     </ListBoxItem> 
    </ListBox> 
    <TextBox></TextBox> 
    </StackPanel> 

如果你運行這個在.NET 4.5,你可以看到,它只是改變了對焦的顏色,但不是沒有,對焦(它的工作原理。 NET 4.0)。任何想法爲什麼?

編輯:這似乎是List/Combo Box Background And Selected Colours Under .net 4.5的重複。

+0

[列表/組合框背景和.net 4.5下選擇的顏色]的可能重複(http://stackoverflow.com/questions/12007918/list-combo-box-background-and-selected-colours-under-net -4-5) –

回答

1

這些都是關於控件的默認templates,如果它們不像.NET 4中那樣使用系統顏色,那麼這將不會改變任何內容。

+0

這不是一個突破性的改變嗎?無論如何,它看起來像ControlBrushKey仍在使用。 –

+0

@DoronYaacoby:這不是一個突破性的改變,因爲依賴於控件模板中的任何東西都是愚蠢的。這有點像通過反射調用私有方法。 –

+0

由於這是對這個問題的接受和最廣泛的答案,我猜很多人發現這個解決方案是最簡單的,所以它會打破很多應用程序。但無論如何,實現我想要的正確方法是什麼? –

13

請嘗試以下變更被選擇的一個ListBoxItem的背景顏色時,它已失去焦點:

XAML

<ListBox.Resources>  
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }" Color="Orange" />  
</ListBox.Resources> 

C#

listBox.Resources.Add(SystemColors.InactiveSelectionHighlightBrushKey, 
         new SolidColorBrush(Colors.Orange)); 

我希望它爲你工作。

+1

請參閱我的答案,以獲取不涉及更改SystemColors的解決方案。 – bugged87

-2

這個問題的解決是調用

InitializeComponent(); 
+0

拋出異常:「屬性'AreInactiveSelectionHighlightBrushKeysSupported'無法更改。'FrameworkCompatibilityPreferences'類已被封裝。」 – bugged87

+0

@ bugged87將這添加到try/catch塊中。我的程序使用這個,所有的版本看起來像所有版本的Windows – Daniel

+0

當然,添加try/catch處理異常,但這個代碼似乎並沒有使OP的代碼工作。我在Windows 8.1上使用.NET 4.5。 – bugged87

0

這就是我想出了不涉及改變系統顏色或控制模板前添加

FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false; 

。只需將ListBox包裝在新的UserControl中。

public partial class StyledListBox : UserControl 
{ 
    public DataTemplate ItemTemplate 
    { 
     get { return (DataTemplate)GetValue(ItemTemplateProperty); } 
     set { SetValue(ItemTemplateProperty, value); } 
    } 

    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable)GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    public object SelectedItem 
    { 
     get { return GetValue(SelectedItemProperty); } 
     set { SetValue(SelectedItemProperty, value); } 
    } 

    public StyledListBox() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(StyledListBox), new FrameworkPropertyMetadata(null)); 
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(StyledListBox), new FrameworkPropertyMetadata(null)); 

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(StyledListBox), new FrameworkPropertyMetadata(null) 
    { 
     BindsTwoWayByDefault = true, 
     DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
    }); 
} 

XAML:

<UserControl x:Class="StyledListBox" 

    <ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}" 
       SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}"> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Border> 
        <Border.Style> 
         <Style TargetType="{x:Type Border}"> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" 
              Value="True"> 
            <Setter Property="Background" Value="Red" /> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Border.Style> 

        <ContentPresenter ContentTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StyledListBox}}}" /> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</UserControl> 

然後簡單地使用包裝用戶控件,好像它是一個列表框。任何其他想要控制的ListBox屬性都可以簡單地添加到包裝中,方法與我的示例中的ItemsSourceSelectedItem相同。