Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to highlight selected row in WPF Listview control with alternating background
    text
    copied!<p>I am using VS 2008 with 3.5 of the .NET framework. I have set up my code to alternate the background lines in a WPF ListView control. One color used is white. The other color used is a light shade of green. When a white line is clicked on, it highlights the line in light blue. When a green line is clicked on, there is no highlighting and the background color remains light green. I have tried specifying the HighlightBrushKey and the ControlBrushKey in XAML, but they had no effect. What do I need to do in order for the green lines to be highlighted when clicked on?</p> <p>Here is the XAML code:</p> <pre><code>&lt;!-- Define the resource for the alternating background background used in the ListView objects. --&gt; &lt;StackPanel.Resources&gt; &lt;Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}"&gt; &lt;Style.Resources&gt; &lt;!-- Foreground for Selected ListViewItem --&gt; &lt;!-- &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/&gt; --&gt; &lt;!-- Background for Selected ListViewItem --&gt; &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/&gt; &lt;SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Brown"/&gt; &lt;/Style.Resources&gt; &lt;Style.Triggers&gt; &lt;!-- Tried setting the background property here, but this did not work. &lt;DataTrigger Binding="{Binding Path=RowSelected}" Value="True"&gt; &lt;Setter Property="Background" Value="Gainsboro" /&gt; &lt;Setter Property="FontWeight" Value="Bold" /&gt; &lt;/DataTrigger&gt; --&gt; &lt;!-- setting up triggers for alternate background colors --&gt; &lt;Trigger Property="ItemsControl.AlternationIndex" Value="1"&gt; &lt;Setter Property="Background" Value="#FFD9F2BF"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;!-- setting up triggers for alternate background colors &lt;Trigger Property="ItemsControl" Value="1"&gt; &lt;Setter Property="Background" Value="#FFD9F2BF"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; --&gt; &lt;Trigger Property="ItemsControl.AlternationIndex" Value="2"&gt; &lt;Setter Property="Background" Value="White"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;!-- setting row height here --&gt; &lt;/Style&gt; &lt;/StackPanel.Resources&gt; &lt;ListView x:Name="ListView1" ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" AlternationCount="2" SelectionChanged="ListView1_SelectionChanged" SelectionMode="Multiple" HorizontalAlignment="Left" ItemsSource = "{Binding ElementName=LobbyWindow, Path=ListCollection1}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Game}"&gt; &lt;GridViewColumnHeader Content="Game" FontWeight="Bold" /&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Stakes}"&gt; &lt;GridViewColumnHeader Content="Stakes" Width="68" FontWeight="Bold" /&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Width="30" DisplayMemberBinding="{Binding Seats}"&gt; &lt;GridViewColumnHeader Content="Seats" FontWeight="Bold" /&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>I also have a Selection_Changed event defined in ListView1, but don't know how to grab the correct property or view element out in order to change the background color for that line:</p> <pre><code>private void ListView1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { ListView listview = sender as ListView; } </code></pre> <p>Any suggestions in XAML or C# code behind would be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload