Tuesday, 27 August 2013

Keeping coustum style of ListBoxItem when selected

Keeping coustum style of ListBoxItem when selected

I have a ListBox where the background color of the items are bound to some
property of the entry:
<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>
This works, but when I mouse over or select an item the background changes
(unsurprisingly perhaps) to the default mouse over / selected color.
I'm new to WPF and I'm not sure I'm going about doing this kind of thing
correctly, I thought maybe I need to use ItemContainerStyleSelector, but
I'm confused as to how to use it, and it seems silly to have to create a
class just for this small thing...
What I also thought was to create an IValueConverter from boolean to the
color, and then bind though it without having to use DataTrigger as a
different approach, woud that be more elegant? would that some how help me
with this problem?

No comments:

Post a Comment