I was trying to find a good solution and tried this one out but sadly it did not work. For anyone in the same boat and happening to find themselves here, I found two really solid approaches. There are many OTHER approaches to this problem, however I wanted to avoid use of any additional DLLs (aka use of interactivity
) or other frameworks.
Attached Behavior
A user by the handle dlf posted an excellent answer here:
GridView DoubleClick
He posted an awesome attached behavior and I have tested it under multiple conditions and it works very well.
Pure XAML
I personally try to do as much as I can in XAML. Essentially I create a ControlTemplate
for the ListViewItem
. The ControlTemplate
needs to be tweaked to both contain whatever highlight color effect you want and must also contain the data for whatever ever row you are selecting. The great part about this is works well even with multiple columns. You will also need a separate ControlTemplate
for each Command
.
Here is an example with a 2 column ListView
and a gradient highlight effect that bound to collection. One thing to make note of in my ControlTemplate
, I set the TextBlock
width to match the width of the column.
Hope this helps anyone looking.
Control Template:
<ControlTemplate x:Key="Selected_Item_Template" TargetType="{x:Type ListViewItem}">
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="19" Margin="0,0,0,0" BorderBrush="LightGray" BorderThickness="1">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF74A1C7" Offset="0.5"/>
<GradientStop Color="SteelBlue" Offset="0.5"/>
</LinearGradientBrush>
</Border.Background>
<Border.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
CommandParameter="{Binding SelectedItems, ElementName=your_listview}"
Command="{Binding Path=DataContext.Some_DoubleClick_ICommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
</Border.InputBindings>
<DockPanel>
<TextBlock Width="{Binding ActualWidth, ElementName=column_1}" Margin="6,0,0,0" Text="{Binding Column1}" />
<TextBlock Margin="0,0,0,0" Text="{Binding Column2}" />
</DockPanel>
</Border>
</ControlTemplate>
The ListView:
<ListView x:Name="your_listview"
ItemsSource="{Binding Some_Source_Collection}"
SelectedIndex="{Binding Some_Int_Variable}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="White" />
<Setter Property="Template" Value="{StaticResource Selected_Item_Template}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="White" />
<Setter Property="Template" Value="{StaticResource Selected_Item_Template}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView x:Name="your_listview_gridview">
<GridViewColumn x:Name="column_1" Header="Name:" Width="100"
DisplayMemberBinding="{Binding Column1}">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="True"/>
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
<GridViewColumn x:Name="column_2" Header="Path:" Width="250"
DisplayMemberBinding="{Binding Column2}">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="IsEnabled" Value="True"/>
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
PM me if you have questions about or if you need help casting SelectedItems
into something usable.