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

uajslkp6  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(121)

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

<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;
        }
    }
mrphzbgm

mrphzbgm1#

一个根本的解决方案是在光标进入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;
    }

slmsl1lt

slmsl1lt2#

执行此操作可强制将鼠标光标置于夹持器上,而不覆盖模板

public static void ColumnHeaderGripper(DataGrid datagrid)
{
    var columnpresenter = typeof(DataGrid)
        .GetProperty("ColumnHeadersPresenter", 
                  BindingFlags.NonPublic | BindingFlags.Instance);

    var headerpresenter = (DataGridColumnHeadersPresenter)
        columnpresenter.GetValue(datagrid);

    for (var i = 1; i < headerpresenter.Items.Count; i++) {

        var columnheader = (DataGridColumnHeader)
            headerpresenter.ItemContainerGenerator.ContainerFromIndex(i);

        var leftgripper = typeof(DataGridColumnHeader)
            .GetField("_leftGripper", 
                   BindingFlags.NonPublic | BindingFlags.Instance);

        var rightgripper = typeof(DataGridColumnHeader)
            .GetField("_rightGripper", 
                       BindingFlags.NonPublic | BindingFlags.Instance);

        var leftThumb = (Thumb)leftgripper.GetValue(columnheader);
        var rightThumb = (Thumb)rightgripper.GetValue(columnheader);

        leftThumb.MouseEnter += (s, e) => { 
            System.Windows.Input.Mouse.OverrideCursor = Cursors.SizeWE; };
        leftThumb.MouseLeave += (s, e) => { 
            System.Windows.Input.Mouse.OverrideCursor = Cursors.Arrow; };

        rightThumb.MouseEnter += (s, e) => { 
            System.Windows.Input.Mouse.OverrideCursor = Cursors.SizeWE; };
        rightThumb.MouseLeave += (s, e) => { 
            System.Windows.Input.Mouse.OverrideCursor = Cursors.Arrow; };
    }
}

相关问题