2013-08-27 47 views
0

我有一個列表框,其中項目的背景顏色綁定到項的某些屬性:飼養coustum風格時選擇

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" > 
    <ListBox.ItemContainerStyle > 
     <Style TargetType="ListBoxItem" > 
      <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true"> 
        <Setter Property="Background" Value="Green" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

這個工作,但是當我鼠標或選擇項目背景變化(可能不會令人驚訝)默認的鼠標懸停/選擇的顏色。我是新來的WPF,我不知道我正在做正確的這種事情,我想也許我需要使用ItemContainerStyleSelector,但我很困惑如何使用它,並且似乎很愚蠢的創建一個類只是爲了這個小東西...

我還以爲是創建一個IVALUEConverter從布爾到顏色,然後綁定,而不必使用DataTrigger作爲一種不同的方法,那會更優雅嗎?會有一些如何幫助我解決這個問題?

編輯:

這也將是很好,如果我可以選擇的項目的背景顏色更改爲基於AnotherPropertyOfFoo不同的顏色,如果不是過分的要求

編輯2 (延伸到@Sheridan回答評論):

這不起作用

<ListBox> 
     <ListBox.Items> 
      <ListBoxItem>one</ListBoxItem> 
      <ListBoxItem>two</ListBoxItem> 
      <ListBoxItem>three</ListBoxItem> 
      <ListBoxItem>four</ListBoxItem> 
     </ListBox.Items> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Background" Value="Green" /> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" /> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" /> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 
       </Style.Resources> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 

回答

0

嘗試使用這樣的:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" > 
    <ListBox.ItemContainerStyle > 
     <Style TargetType="ListBoxItem" > 
      <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding AnotherPropertyOfFoo}" Value="true"> 
        <Setter Property="Background" Value="Green" /> 
       </DataTrigger> 
      </Style.Triggers> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 
      </Style.Resources> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

SystemColors.HighlightBrushKey代表集控所選項目的默認背景色......這裏被設置爲Transparent,但你可以將其設置到過顏色,你喜歡。

UPDATE >>>

此代碼工作就好了...如果你在Resources部分改變第一SolidColorBrushRed,那麼所選擇的項目的背景顏色將是Red。您的BindingAnotherPropertyOfFoo不會影響所選項目,因爲兩者之間沒有關係。爲了實現這一目標,可以改爲嘗試這個辦法:

<ListBox ItemsSource="{Binding ObservableCollectionOfFoos}" > 
    <ListBox.ItemContainerStyle > 
     <Style TargetType="ListBoxItem" > 
      <Setter Property="Content" Value="{Binding SomePropertyOfFoo}"/> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="{Binding AnotherPropertyOfFoo}" /> 
       </DataTrigger> 
      </Style.Triggers> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 
      </Style.Resources> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

現在所選擇的項目將獲得來自AnotherPropertyOfFoo財產的背景顏色。

+0

嗨,感謝您的回覆,但這似乎沒有任何效果。 – pseudoDust

+0

對不起,我不確定我在做什麼錯,但是這根本沒有任何效果。我已經隔離了最簡單的例子,我可以創建它,但它仍然不起作用,請參閱我使用的代碼編輯我的文章,也許你可以找到我的錯誤 – pseudoDust

1

您也可以重寫ListBoxItem的模板,使用混合提取默認值並覆蓋或使用some這裏已經提到。

編輯

其實這是不是很難覆蓋模板:),我想這是解決你的問題最正確的方法。試試這個ItemContainer樣式。它取代了默認的ListBoxItem樣式 - >模板。要看看它是如何工作的 - 在觸發器中,您可以更改列表框項目的任何屬性。

<ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Background" Value="Transparent"/> 
       <Setter Property="Padding" Value="2,0,0,0"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListBoxItem"> 
          <Border Name="Border" Padding="2" SnapsToDevicePixels="true"> 
           <ContentPresenter /> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsMouseOver" Value="true"> 
            <Setter TargetName="Border" Property="Background" Value="LightBlue"/> 
           </Trigger> 
           <Trigger Property="IsSelected" Value="true"> 
            <Setter TargetName="Border" Property="Background" Value="Blue"/> 
           </Trigger> 
           <Trigger Property="IsEnabled" Value="false"> 
            <Setter Property="Foreground" Value="Gray"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 

ListBoxItem的默認樣式,您可以找到here進行必要的修改。

+0

我不使用混合,說實話,我寧願避免這個解決方案,因爲它看起來像是用一種戰術核武器殺死一隻蒼蠅......必須有一個簡單的海峽前進方式,不是嗎? – pseudoDust

+1

從編輯後的文章中抓取樣式並嘗試進行實驗。希望這可以幫助。 –

+0

好的,謝謝,但我仍然想將顏色綁定到布爾屬性,我必須使用IValueConverter嗎? – pseudoDust