0

I have a Student object which has:

private string name;
private double feesOwed;

I also have a ListBox which will have an ItemSource set to a List object populated with several Students. I would like to have the ListBox display the name as its Text and have a background coloured based on feesOwed. Something like

if(feesOwed>20) 
{
     if(feesOwed>100)
     {
            item.Background = "Red";
            return;
     }
     item.Background = "Yellow";
     return;
}

All of the examples I've found about this mostly just talk about how to get alternating row colours. I know this will require Data Binding but the topic is fairly new to me and I can't quite get it to work dynamically.

I think the correct way to do this is to implement IValueConverter but this is also a little daunting.

Thanks

Jacob Patenaude
  • 129
  • 1
  • 1
  • 10
  • I've no time to work out an example. What you need is a `ItemTemplate` for the listbox. There you bind the background color of an item to the item its `feesOwed` using a converter, see `IValueConverter`. The converter returns the color based on the `feesOwed` value. See this for example: http://blogs.msdn.com/b/bencon/archive/2006/05/10/594886.aspx – Mike de Klerk Oct 17 '14 at 07:42

1 Answers1

3

you can use data triggers as shown below

     <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <DataTrigger Binding="{Binding feesOwed}" Value="20">
                <Setter Property="Background" Value="Yellow"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding feesOwed}" Value="100">
                <Setter Property="Background" Value="Red"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

you can refer this link

Community
  • 1
  • 1
Vikram
  • 1,617
  • 5
  • 17
  • 37