2013-08-27 46 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