Reputation:
I programmatically fill my listview with Strings
using a foreach statement without using .itemssource and then i created this style trigger (which appears to be correct)
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="True" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
And then i run the project and click on a list view item... and the background is blue ........
I'm Just wondering if style triggers require the use of databinding... or if my trigger is wrong.. or if anyone has any ideas.. ???
Upvotes: 1
Views: 159
Reputation: 128098
Your problem is not related to binding, and the Trigger is also OK, except that it will be ineffective.
The background color of a selected ListViewItem is set by the VisualStateManager in the ListViewItem's ControlTemplate like show in the MSDN example:
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border" Background="Transparent" ...>
<VisualStateManager.VisualStateGroups>
...
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedBackgroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
...
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
No matter to what value you set the item's background, it will be set to the value of {StaticResource SelectedBackgroundColor}
when the item's state is Selected
. You might however add the following lines to your ListViewItem style to override the value of the SelectedBackgroundColor resource:
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</Style.Resources>
...
</Style>
Upvotes: 1
Reputation: 8907
The trigger works fine, but the problem is that the control template of the ListViewItem does not actually use the value of the Background property as the background color of the control.
Instead it uses the value of the SystemColors.HighlightBrush property, which by default is blue (so it always looks like a selection usually does in Windows).
That property has a resource key associated with it, so you can just define a new Brush with the same key and then that one will be used by the ListView instead. And now can get rid of the Trigger as well.
<ListView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
</ListView.Resources>
Upvotes: 1