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

时间:2014-09-26 12:54:18

标签: c# wpf datagrid grouping expander

我已经修改了Expander的样式,以便能够在DataGrid中扩展组标题中的内容。该样式基于MSDN example模板。它工作正常,但当我用鼠标滚轮滚动时程序冻结。使用滚动条滚动工作正常!任何人都可以看到有什么问题吗?

<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中我使用DataGrid和VirtualizingPanel.IsVirtualizingWhenGrouping =&#34; True&#34;表现。

<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>

1 个答案:

答案 0 :(得分:1)

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

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

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

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

我看到问题中的XAML在GroupItem的样式上也有一个可以改变其高度的余量。也许那是罪魁祸首?

希望这有助于下一个人。