tags:

views:

2464

answers:

4

How do I set the background colour of items in a list box dynamically? i.e. there is some property on my business object that I'm binding too, so based on some business rules I want the background colour to be different?

        <ListBox Background="Red">
      <ListBox.ItemContainerStyle>
          <Style TargetType="ListBoxItem">
              <Setter Property="Background" Value="Red"/>
          </Style>
      </ListBox.ItemContainerStyle>
 <ListBox.ItemTemplate>
      <DataTemplate>
                    <StackPanel Orientation="Horizontal"
                                Margin="5">
                        <TextBlock VerticalAlignment="Bottom"
                                   FontFamily="Comic Sans MS"
                                   FontSize="12"
                                   Width="70"
                                   Text="{Binding Name}" />
                        <TextBlock VerticalAlignment="Bottom"
                                   FontFamily="Comic Sans MS"
                                   FontSize="12"
                                   Width="70"
                                   Text="{Binding Age}" />
                     </StackPanel>
                </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

EDIT: It says here

In Silverlight, you must add x:Key attributes to your custom styles and reference them as static resources. Silverlight does not support implicit styles applied using the TargetType attribute value.

Does this impact my approach?

A: 

@Matt Thanks for the reply. I'll look into triggers.

My only problem is that, the logic for determining whether a row should be coloured is slightly more involved so I cant just checking a property, so I actually need to run some logic to determine the colour. Any ideas?

I guess I could make a UI object with all the relevant fields I need, but I kinda didnt want to take the approach.

Dan
+2  A: 

Ok - if you need custom logic to determine the background then I would look into building a simple IValueConverter class. You just need to implement the IValueConverter interface and, in its Convert method, change the supplied value into a Brush.

Here's a quick post from Sahil Malik that describes IValueConverters - it might help:

http://blah.winsmarts.com/2007-3-WPF__DataBinding_to_Calculated_Values--The_IValueConverter_interface.aspx

Matt Hamilton
+1  A: 

To bind your background to more than one property, you can use IMultiValueConverter. It's just like IValueConverter except that it works with MultiBinding to pass more than one value into a class and get back a single value.

Here's a post I found with a run-through on IMultiValueConverter and MultiBinding:

http://blog.paranoidferret.com/index.php/2008/07/21/wpf-tutorial-using-multibindings/

Edit: If IMultiValueConverter isn't available (it looks like Silverlight only has IValueConverter) then you can always pass your entire bound object (eg your Person object) to an IValueConverter and use various properties from that to return your Brush.

Matt Hamilton
A: 

You could try binding something in your controltemplate (ie a border or something) to the TemplateBackground. Then set the background on your listbox to determine the colour it will be.

<Border Margin="-2,-2,-2,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" CornerRadius="11,11,0,0">
Stephen Price