如何永久更改WPF GridViewColumnHeader抓取器光标?

时间:2018-03-20 13:12:28

标签: wpf xaml controltemplate

我可以将其更改为SizeWE Loaded,Initialized,MouseEnter事件处理程序,但在列调整大小后,它将更改回默认值。试图改变它,例如SizeWE的MouseUp或DragCompleted事件处理程序不起作用。在调整大小结束时释放鼠标按钮时,光标将更改为默认值。当在MouseEnter处理程序中完成SizeWE更改时,当鼠标退出并再次进入时,它会再次出现,但问题是在调整大小之后。

XAML:

   <Style x:Key="GridView.ColumnHeader.Gripper.Style" TargetType="{x:Type Thumb}" BasedOn="{StaticResource Theme.DataGrid.ColumnHeader.Gripper.Style}">
        <Setter Property="Width" Value="20" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Cursor" Value="SizeWE" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type GridViewColumnHeader}" >
        <Setter Property="FontWeight" Value="{StaticResource Theme.DataGrid.ColumnHeader.FontWeight}"></Setter>
        <Setter Property="BorderBrush" Value="Transparent"></Setter>
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="Background" Value="{StaticResource Theme.DataGrid.ColumnHeader.Background}"></Setter>
        <Setter Property="Foreground" Value="{StaticResource Theme.DataGrid.ColumnHeader.Foreground}"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Left"></Setter>
        <Setter Property="Padding" Value="{StaticResource  Theme.DataGrid.Cell.Padding}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0"  x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}">
                            <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                        <Thumb Grid.Column="1" x:Name="PART_HeaderGripper" HorizontalAlignment="Right"  
                               Style="{DynamicResource GridView.ColumnHeader.Gripper.Style}" 
                               Cursor="SizeWE" 
                               MouseUp="PART_HeaderGripper_MouseUp" 
                               MouseEnter="PART_HeaderGripper_MouseEnter"
                               Margin="-18,0, 0, 0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
代码背后的代码:

    private void PART_HeaderGripper_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Thumb gripper = (Thumb)sender;
        if (gripper != null)
        {
            gripper.Cursor = Cursors.SizeWE;
        }
    }
    private void PART_HeaderGripper_MouseEnter(object sender, MouseEventArgs e)
    {
        Thumb gripper = (Thumb)sender;
        if (gripper != null)
        {
            gripper.Cursor = Cursors.SizeWE;
        }
    }

1 个答案:

答案 0 :(得分:2)

一个根本的解决方案是在光标进入Thumb时设置Mouse.OverrideCursor,并在光标离开时将其设置回来:

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0"  x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}"
                    Background="{TemplateBinding Background}">
                            <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                        <Thumb MouseEnter="UIElement_OnMouseEnter" MouseLeave="UIElement_OnMouseLeave" Grid.Column="1" x:Name="PART_HeaderGripper" HorizontalAlignment="Right"  
                           Style="{DynamicResource GridView.ColumnHeader.Gripper.Style}"                                
                           Margin="-18,0, 0, 0"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

处理程序:

private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
    {
        Mouse.OverrideCursor = Cursors.SizeWE;
    }

    private void UIElement_OnMouseLeave(object sender, MouseEventArgs e)
    {
        Mouse.OverrideCursor = null;
    }

enter image description here