Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using gridview in listview control i tried to change the mouseover color of GridViewColumnHeader and to remove glow effect.
i will give the code below what i tried

What I have tried:

<Style x:Key="GridViewColumnHeaderStyle1" TargetType="{x:Type GridViewColumnHeader}">
           <Style.Triggers>
               <Trigger Property="IsMouseOver" Value="true">
                   <Setter Property="Background" Value="#EFEFEF"/>
                   <Setter Property="Foreground" Value="#EFEFEF"/>
               </Trigger>
           </Style.Triggers>
       </Style>




 <ListView>
<ListView.View>
<GridView   ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle1}">
<GridViewColumn>
<GridViewColumn.Header>
 <GridViewColumnHeader Margin="-3,0,0,0" Tag="Name" Background="#EFEFEF"  BorderThickness="0"  Width="auto" Height="40" >
<TextBlock Text="Name" />
</GridViewColumnHeader>

</GridViewColumn.Header>
</GridViewColumn>

</GridView>
</ListView.View>
</Listview>
Posted
Updated 30-May-21 5:26am

1 solution

If I understand the situation correctly it would perhaps be easiest to use window level resource definition and set the visual effects over there. Something like
XML
<Window.Resources>
    <Style TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Margin="-3,0,0,0" Tag="Col 1" BorderThickness="0"  Width="auto" Height="40" Content="Col 1" />
                    </GridViewColumn.Header>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Margin="-3,0,0,0" Tag="Col 2" BorderThickness="0"  Width="auto" Height="40" Content="Col 2"/>
                    </GridViewColumn.Header>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

If you want a more complex visualization for the header, then consider using a ControlTemplate. For more information, see How to create a template - WPF .NET | Microsoft Docs[^]

EDIT: Modified version using control template

XML
<Window.Resources>
    <ControlTemplate x:Key="CustomHeader" TargetType="GridViewColumnHeader">
        <Grid Background="{TemplateBinding Background}" Height="40">
            <TextBlock Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Tag}" VerticalAlignment="Center" />
            <Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Margin="0" Width="1"/>
        </Grid>
    </ControlTemplate>
    <Style TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="#EFEFEF"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="Green"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Template="{StaticResource CustomHeader}" Margin="-3,0,0,0" BorderThickness="0" Tag="Col 1" />
                    </GridViewColumn.Header>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.Header>
                        <GridViewColumnHeader Template="{StaticResource CustomHeader}" Margin="-3,0,0,0" BorderThickness="0" Tag="Col 2" />
                    </GridViewColumn.Header>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
 
Share this answer
 
v3
Comments
Fazil13 30-May-21 11:46am    
Above solution only the content color is changing i need to change the background while mouse over it is changing into default window's color
Wendelius 30-May-21 12:41pm    
In more complex situations you may want to use control templates or create even your own UI classes. See the modified answer.
Fazil13 30-May-21 12:46pm    
One more doubt now cant able to resize the coloumns using mouse,Rest of the things are working thanks for your valuble time.
Wendelius 30-May-21 13:01pm    
That's correct. The control template is replacing the whole header control so you need to define the sizing ability in the template. See the updated example where Thumb Class (System.Windows.Controls.Primitives) | Microsoft Docs[^] is added
Fazil13 30-May-21 13:37pm    
Yes got it thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900