WPF-鼠标滚轮滚动冻结自定义扩展器的DataGrid

本文关键字:自定义 扩展器 DataGrid 冻结 滚动 鼠标 WPF- | 更新日期: 2023-09-27 18:24:42

我修改了扩展器的样式,使其能够拉伸DataGrid中组标头中的内容。该样式基于MSDN示例模板。它工作得很好,但当我用鼠标滚轮滚动时,程序会冻结。使用滚动条滚动效果很好!有人能看到出了什么问题吗?

<ControlTemplate x:Key="ExpanderToggleButton"
             TargetType="{x:Type ToggleButton}">
    <Border x:Name="Border"
      CornerRadius="0"
      BorderThickness="0">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CheckStates">
                <VisualState x:Name="Checked">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                       Storyboard.TargetName="CollapsedArrow">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                  Value="{x:Static Visibility.Hidden}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                       Storyboard.TargetName="ExpandededArrow">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                  Value="{x:Static Visibility.Visible}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Unchecked" />
                <VisualState x:Name="Indeterminate" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid>
            <Path x:Name="CollapsedArrow"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Data="M 0 0 L 4 4 L 8 0 Z">
                <Path.Fill>
                    <SolidColorBrush Color="{StaticResource ForegroundColor}" />
                </Path.Fill>
            </Path>
            <Path x:Name="ExpandededArrow"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Visibility="Collapsed"
        Data="M 0 4 L 4 0 L 8 4 Z">
                <Path.Fill>
                    <SolidColorBrush Color="{StaticResource ForegroundColor}" />
                </Path.Fill>
            </Path>
        </Grid>
    </Border>
</ControlTemplate>
<Style TargetType="{x:Type Expander}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Expander}">
                <StackPanel>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Border" Grid.Row="0" BorderThickness="0" CornerRadius="0">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <ToggleButton OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" 
                                        IsChecked="{Binding IsExpanded, Mode=TwoWay, 
                                        RelativeSource={RelativeSource TemplatedParent}}">
                            </ToggleButton>
                            <ContentPresenter  Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True" />
                        </Grid>
                    </Border>
                    <Border x:Name="Content"
                          BorderThickness="0"
                          CornerRadius="0" Visibility="Collapsed">
                        <ContentPresenter />
                    </Border>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="True">
                        <Setter TargetName="Content" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在xaml中,我使用带有VirtualizingPanel的DataGrid。IsVirtualizingWhenGrouping="True"以提高性能。

<DataGrid Margin="10, 5, 10, 10" 
    VirtualizingPanel.IsVirtualizingWhenGrouping="True" >
<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <Grid>
                    <Label HorizontalAlignment="Left">Label to the left</Label>
                    <Button HorizontalAlignment="Right">Button to the right</Button>
                </Grid>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.Panel>
            <ItemsPanelTemplate>
                <DataGridRowsPresenter/>
            </ItemsPanelTemplate>
        </GroupStyle.Panel>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Margin" Value="0,0,0,15"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander Background="Transparent" IsExpanded="True" HorizontalAlignment="Stretch" BorderThickness="0" >
                                <Expander.Header>
                                    <ContentPresenter HorizontalAlignment="Stretch" />
                                </Expander.Header>
                                <Expander.Content>
                                    <ItemsPresenter />
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>

WPF-鼠标滚轮滚动冻结自定义扩展器的DataGrid

回答这个问题很晚,但我在使用IsVirtualizingWhenGrouping的ListBox寻找类似问题的答案时遇到了这个问题。这是我的解决方案。

我的问题是GroupStyle.ContainerStyle有一个改变垂直大小的边距:

<GroupStyle.ContainerStyle>
  <Style TargetType="GroupItem">
    <Setter Property="Margin" Value="0,10,0,0" />
  </Style>
</GroupStyle.ContainerStyle>

对我来说,我只是一起删除了ContainerStyle,并将边距移到了HeaderTemplate。

我看到问题中的XAML在GroupItem的样式上也有一个会改变其高度的边距。也许这就是罪魁祸首?

希望这能帮助下一个人。