Remove glassy effect from rows in WPF ListView
I'm trying to style a ListView in my WPF based application. What I'm trying to do, is to create a gradient effect on the mouseover on rows. But what I don't want is the standard glassy effect that is added to the rows. Even with my own styling (below), the glassy effect is still there.
XAML Style
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<GradientStop Color="#39B4E7" Offset="0"/>
<GradientStop Color="#4297BB" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
ListView
<ListView Grid.Row="0" ItemsSource="{Binding Projects}" SelectedItem="{Binding CurrentProject}" Margin="3" ItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="" DisplayMemberBinding="{Binding ShortName}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Owner" DisplayMemberBinding="{Binding User.Name}" />
<GridViewColumn Header="Versions" DisplayMemberBinding="{Binding Versions.Count}" />
</GridView>
</ListView.View>
</ListView>
New XAML Style
<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<GradientStop Color="#39B4E7" Offset="0"/>
<GradientStop Color="#4297BB" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.开发者_如何转开发Triggers>
</Style>
Is this possible?
Each ListViewItem
resides within a contianer, and that container is what gets the background on selection/hover. So you have to set a value for the the ItemContainerStyle
attribute on the ListView
.
example...
<ListView ItemContainerStyle="{StaticResource ListViewItemContainerStyle}" />
the style..
<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
</Style.Resources>
</Style>
Edit:
So for your case, you can keep everything, just stick that style in your ResourceDictionary
and add the ItemContainerStyle
attribute on the ListView
. Hope this helps, let me know if you get stuck.
This is what I used to do before, but it eventually got tedious so I extended the ListView
control to handle this automatically and added an attribute for the Hover/Selection color.
Updated Code:
<ListView Grid.Row="0" ItemsSource="{Binding Projects}" SelectedItem="{Binding CurrentProject}" Margin="3" ItemContainerStyle="{StaticResource ListViewItemContainerStyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="" DisplayMemberBinding="{Binding ShortName}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Owner" DisplayMemberBinding="{Binding User.Name}" />
<GridViewColumn Header="Versions" DisplayMemberBinding="{Binding Versions.Count}" />
</GridView>
</ListView.View>
</ListView>
** The resource file**
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush>
<GradientStop Color="#39B4E7" Offset="0"/>
<GradientStop Color="#4297BB" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
</Style.Resources>
</Style>
精彩评论