用户通过鼠标调整 WPF DataGrid 列大小时如何获取事件

问题描述 投票:0回答:1

如何获取列大小调整事件通过用户交互调用(鼠标左键+鼠标移动)。

我尝试了SizeChanged事件,但当新项目添加到DataGrid(带有长文本)时也会触发它,但不应处理这种情况。

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <EventSetter Event="SizeChanged" Handler="DataGridCell_SizeChanged" />
    </Style>
</DataGrid.CellStyle>

感谢您的任何建议。

wpf datagrid resize mouseevent
1个回答
0
投票

我使用 Blend 来获取 DataGrid 的样式,一直到

DataGridColumnHeader
Thumb
(DataGrid > DataGridColumnHeadersPresenter > DataGridColumnHeader > Thumb、ColumnHeaderGripperStyle)。然后使用
Thumb x:Name="PART_RightHeaderGripper"
并处理
PreviewMouseLeftButtonDown
PreviewMouseMove
事件。

        <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <Grid>
                            <theme:DataGridHeaderBorder Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" IsClickable="{TemplateBinding CanUserSort}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" SeparatorBrush="{TemplateBinding SeparatorBrush}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </theme:DataGridHeaderBorder>
                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ThumbStyle1}" />
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"
                                   PreviewMouseLeftButtonDown="PART_RightHeaderGripper_PreviewMouseLeftButtonDown"
                                   PreviewMouseMove="PART_RightHeaderGripper_PreviewMouseMove"
                                   />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
© www.soinside.com 2019 - 2024. All rights reserved.