English 中文(简体)
WPF ListView : Header styling
原标题:

I want to have a ListView with columns and a particular style:

The background for ALL column headers should be transparent except when the mouse is over in one of them.

When this happends, the column s background in which the mouse is over should be yellow and all the color in the other columns should be, let s say, blue.

I ve tried playing with GridViewColumnHeader template, but that seems to change only the active column background. Any help?

最佳回答

Finally, I found a way to do this: basically, you set a trigger that will see if the parent GridViewHeaderRowPresenter is selected. All headers will then return true to that property.

Then you check if the header has the mouse over it and only the selected header will return true.

The result will be something like this:

<ControlTemplate.Triggers>
  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                   AncestorType={x:Type GridViewHeaderRowPresenter}},
                                 Path=IsMouseOver}"
               Value="True">
    <Setter TargetName="HeaderBack" Property="Background"
            Value="{StaticResource HeaderActiveColumnBackground}"/>
    <Setter TargetName="PART_HeaderGripper" Property="Background"
            Value="{StaticResource VerticalLineColor}"/>
  </DataTrigger>
  <Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="HeaderBack" Property="Background"
            Value="{StaticResource HeaderSelectedColumnBackground}"/>
  </Trigger>
  <Trigger Property="HasContent" Value="false">
    <Setter TargetName="HeaderBack" Property="Background"
            Value="{StaticResource HeaderDefaultColumnNoContentBackground}"/>
    <Setter TargetName="PART_HeaderGripper" Property="Background"
            Value="{StaticResource HeaderDefaultColumnNoContentBackground}"/>
  </Trigger>
</ControlTemplate.Triggers>
问题回答

In order to do this, I think you re going to have to replace the entire ListView style. Microsoft has an example.

You ll have to put a Border around the GridViewHeaderRowPresenter in the ScrollViewer style shown in there and add an IsMouseOver trigger to set the background of that Border to Blue.

Then, of course, you ll need an IsMouseOver trigger on the GridViewColumnHeader template to make the background yellow.

If you need any further explanation, please ask.

-- HTH, Dusty





相关问题
Creating a Style in code behind

Does anyone know how to create a wpf Style in code behind, I can t find anything on the web or MSDN docs. I have tried this but it is not working: Style s = new Style(typeof(TextBlock)); s....

WPF Custom Themes

I have a simple question which is giving me some difficulty. I have downloaded a custom them for WPF of the net. Now i want to apply this theme to my App instead of the default one. How do i do that,...

Is it sometimes bad to use <BR />?

Is it sometimes bad to use <BR/> tags? I ask because some of the first advice my development team gave me was this: Don t use <BR/> ; instead, use styles. But why? Are there negative ...

WPF - How to apply effect to a cropped image?

I have an Image being clipped like so: <Image Width="45" Grid.Column="0" Source="{Binding Photo}"> <Image.Clip> <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" /...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签