无法在数据网格的第3列中使用虚拟化。我正在将这两种解决方案结合起来
https://www.codeproject.com/Articles/1213466/WPF-TreeGrid-using-a-DataGrid
任何帮助?!
<Window x:Class="TreeGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeGrid"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<GridLength x:Key="TreeToggleWidth">20</GridLength>
<local:LevelConverter x:Key="LevelSpaceConverter" LevelWidth="{StaticResource TreeToggleWidth}"/>
<local:VisibilityConverter x:Key="TreeToggleConverter"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0"/>
<DataGrid AutoGenerateColumns="False" Name="grid" Grid.Row="1" CanUserAddRows="False"
AlternationCount="2" AlternatingRowBackground="LightGray" ItemsSource="{Binding Model.FlatModel}" EnableColumnVirtualization="True"
ScrollViewer.CanContentScroll="True">
<DataGrid.Columns>
<DataGridTemplateColumn
Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Level, Converter={StaticResource LevelSpaceConverter}}"/>
<ColumnDefinition Width="{StaticResource TreeToggleWidth}"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Background="Red" Grid.Column="0"/>
<CheckBox
Grid.Column="1"
IsChecked="{Binding IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding HasChildren, Converter={StaticResource TreeToggleConverter}}"/>
<TextBlock
Grid.Column="2"
Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn
Header="Value"
Binding="{Binding Value}"/>
<DataGridTemplateColumn >
<DataGridTemplateColumn.HeaderStyle>
<Style
TargetType="DataGridColumnHeader">
<Setter
Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter
Property="VerticalContentAlignment"
Value="Stretch" />
<Setter Property="Margin"
Value="0" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ItemsControl VirtualizingStackPanel.IsVirtualizing="True"
ScrollViewer.CanContentScroll="True"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Headers}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.VirtualizationMode="Recycling">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="70" >
<TextBlock Text="{Binding}"
TextAlignment="Center"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl VirtualizingStackPanel.IsVirtualizing="True"
ScrollViewer.CanContentScroll="True"
ItemsSource="{Binding TS}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.VirtualizationMode="Recycling">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="70" >
<TextBlock Text="{Binding}"
TextAlignment="Center"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
答案 0 :(得分:0)
即使使用VirtualizingStackPanel作为其ItemsPanel,ItemsControl也不支持虚拟化。您必须将其交换为支持它的项目,例如ListView,请参阅here。
此外,我必须删除ScrollViewer,而是向VirtualizingStackPanel添加一个Width以使虚拟化工作:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" Width="400"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Border Width="70" >
<TextBlock Text="{Binding}"
TextAlignment="Center"/>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
通过这些更改,我得到了一个很好的,虚拟化的第三列(对于内容,我没有对标题做同样的事情。同样也应该在那里工作)。