放置在scrollviewer中时,WPF Datagrid虚拟化被禁用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了放置在scrollviewer中时,WPF Datagrid虚拟化被禁用相关的知识,希望对你有一定的参考价值。
我有一个场景,我在使用两个数据网格基本上实现分组,如下所示。为了实现两个网格之间的同步滚动,我正在使用滚动查看器。使用滚动查看器会禁用虚拟化,并且数据网格中的加载数据会受到严重影响。下面是我正在使用的示例代码。任何建议都应该是有帮助的。
<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
滚动查看器正在禁用虚拟化。我需要在datagrid的某些列中合并单元格。请告诉我任何替代解决方案,以实现相同而不失去虚拟化。
答案
仅当网格的大小受限时,虚拟化才有效。
如果将DataGrid放在ScrollViewer或StackPanel或任何未将修复约束传递给DataGrid的控件中,它将破坏DataGrid中的虚拟化。
另一答案
如果你能给我一些有用的例子会更好,但无论如何使用ScrollViewer作为DataGrid的父亲并不是一个好主意,我认为覆盖DataGrid并拥有scrollValue的依赖属性会更好,你应该有独立的滚动条并将此DataGrid的依赖项属性绑定到此滚动条值,这样您可以在滚动条值更改时通过代码滚动两个DataGrids,并且您的虚拟化应该可以工作。
你好。我在github中添加了代码,请下载并尝试https://github.com/Ilya-Grigoryan/SynchDataGridScroll.git
以上是关于放置在scrollviewer中时,WPF Datagrid虚拟化被禁用的主要内容,如果未能解决你的问题,请参考以下文章