WPF Datagrid virtualization getting disabled when placed in a scrollviewer

╄→尐↘猪︶ㄣ 提交于 2021-01-29 06:11:42

问题


I have a scenario where in I am using two datagrids basically to achieve grouping as shown below. For achieving synchronized scrolling between two grids I am using a scrollviewer. Using scroll viewer is disabling the virtualization and loading data in datagrids is getting affected drastically. below is the sample code I am using. Any suggestions whould be of gr8 help.

                            <Grid Name="Test" Background="White">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                    <DataGrid Name="DataGrid1"  Grid.Column="0" AutoGenerateColumns="False" CanUserResizeRows="False"
              CanUserAddRows="False" BorderBrush="#FF1B1A1A" BorderThickness="0" Background="{x:Null}"  HorizontalGridLinesBrush="DarkGray"
      VerticalGridLinesBrush="DarkGray" SelectionChanged="basketNameDataGrid_SelectionChanged" PreviewMouseWheel="basketNameDataGrid_PreviewMouseWheel">
                                    <DataGrid.CellStyle>
                                        <Style TargetType="DataGridCell">
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected"
                    Value="True">
                                                    <Setter Property="Background"
                    Value="#1E90FF" />
                                                    <Setter Property="Foreground"
                    Value="White" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </DataGrid.CellStyle>
                                    <DataGrid.RowStyle>
                                        <Style TargetType="DataGridRow">
                                            <Setter Property="Height" Value="{Binding RowHeight}"></Setter>
                                        </Style>
                                    </DataGrid.RowStyle>
                                    <!--<DataGrid.RowStyle>
                                        <Style TargetType="DataGridRow">
                                            <Setter Property="Height" Value="{Binding RowHeight}"></Setter>
                                        </Style>
                                    </DataGrid.RowStyle>-->
                                    <DataGrid.Columns>
                                        <!--<DataGridTemplateColumn>
                                <DataGridTemplateColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Vertical" >
                                                <Border Margin=".5" >
                                                    <Button Content="Signal" KeyboardNavigation.IsTabStop="False"/>
                                                </Border>
                                                <StackPanel Orientation="Horizontal">
                                                    <Border Margin="0" >
                                                        <Button Margin="0" >
                                                            <TextBlock Text="Name" />
                                                        </Button>
                                                    </Border>
                                                    <Border >
                                                        <Button  KeyboardNavigation.IsTabStop="False" >
                                                            <TextBlock MinWidth="90" MinHeight="27" Text="Description" Margin="10,10,0,0" />
                                                        </Button>
                                                    </Border>
                                            </StackPanel>

                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                </DataGridTemplateColumn>-->
                                        <DataGridTemplateColumn Header="Object Reference" >
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding ObjRef}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="by ICD">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding ByICD}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="by Standard" >
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding ByStandard}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                                    <DataGrid Name="DataGrid2" Grid.Column="1" HeadersVisibility="Column" 
              CanUserResizeRows="False" CanUserAddRows="False" Margin="0" BorderBrush="{x:Null}" BorderThickness="0" Background="{x:Null}"
                                           HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" SelectionChanged="itemDataGrid_SelectionChanged" PreviewMouseWheel="itemDataGrid_PreviewMouseWheel">
                                    <DataGrid.CellStyle>
                                        <Style TargetType="DataGridCell">
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected"
                    Value="True">
                                                    <Setter Property="Background"
                    Value="#1E90FF" />
                                                    <Setter Property="Foreground"
                    Value="White" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </DataGrid.CellStyle>
                                    <DataGrid.RowStyle>
                                        <Style TargetType="DataGridRow">
                                            <Setter Property="Height" Value="20"></Setter>
                                        </Style>
                                    </DataGrid.RowStyle>
                                    <DataGrid.Columns>
                                        <DataGridTemplateColumn Header="Attribute" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Attribute, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Type" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Type, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="FC" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding FC, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Default" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding DefaultVal, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>

                                        <DataGridTemplateColumn Header="Table Type" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding TableType, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Signal Name" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Name, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Signal Description" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Desription, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <!--<DataGridTemplateColumn>
                                    <DataGridTemplateColumn.HeaderTemplate>
                                        <DataTemplate>
                                            <ItemsControl Height="36" Width="175">-->
                                        <!--ItemsSource="{Binding DataContext.ScenarioHeaders, ElementName=LayoutRoot}">-->
                                        <!--
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal" />
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <StackPanel Orientation="Vertical">
                                                            <Border  Width="150" >
                                                                <TextBlock Text="Description" TextAlignment="Center"/>
                                                            </Border>
                                                            <StackPanel Orientation="Horizontal">
                                                                <Border  Width="50" >
                                                                    <TextBlock Text="Signal2" TextAlignment="Center">

                                                                    </TextBlock>
                                                                </Border>
                                                                <Border  Width="50" >
                                                                    <TextBlock Text="Signal1" TextAlignment="Center"/>
                                                                </Border>
                                                            </StackPanel>
                                                        </StackPanel>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.HeaderTemplate>
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ItemsControl ItemsSource="{Binding ClassSegments}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal"/>
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <StackPanel Orientation="Horizontal">
                                                            <Border  Width="50" >
                                                                <TextBlock/>
                                                                -->
                                        <!--Text="{Binding RegularStudentCount}" TextAlignment="Center"/>-->
                                        <!--
                                                            </Border>
                                                            <Border  Width="50" >
                                                                <TextBlock></TextBlock> -->
                                        <!--Text="{Binding EIPStudentCount}" TextAlignment="Center"/>-->
                                        <!--
                                                            </Border>
                                                        </StackPanel>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>-->
                                        <DataGridCheckBoxColumn Header="Inverse" Width="Auto" Binding="{Binding Inverse, Mode = TwoWay}" >
                                            <!--<DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Inverse, Mode = OneWay}"/>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>-->
                                        </DataGridCheckBoxColumn>
                                        <DataGridTemplateColumn Header="Period(ssp)" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding period, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Timing" Width="Auto">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Timing, Mode = OneWay}" HorizontalAlignment="Center"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </Grid>

                        </ScrollViewer>
                        </Grid> 

Sample image of two merged datagrids

Scroll viewer is disabling virtualization. I need to have merged cells in some columns of my datagrid. Please let me know of any alternate solution to achieve the same without losing virtualization.


回答1:


Virtualization only works if the grid is constrained in size.

If you place DataGrid inside ScrollViewer or StackPanel or any control that does not pass fix constraint to DataGrid it will break the virtualization in DataGrid.




回答2:


it will be better if you can give me a little working example, but anyway using ScrollViewer as parent of DataGrid is not a good idea, I think it will be better to override DataGrid and have dependency property for scrollValue, and you should have independent scrollbar and bind this dependency property for both DataGrid to this scrollbar value, in this way you can scroll both DataGrids via code behind while scrollbar value changed, and you virtualization should work.

Hi. I have added code in github, please download it and try https://github.com/Ilya-Grigoryan/SynchDataGridScroll.git



来源:https://stackoverflow.com/questions/52221666/wpf-datagrid-virtualization-getting-disabled-when-placed-in-a-scrollviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!