我有这种风格的一个数据网格:
I have this style for a data grid:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
Grid.Column="1"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
CanContentScroll="{TemplateBinding CanContentScroll}"
Grid.ColumnSpan="2"
Grid.Row="1" />
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Column="2"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
Grid.Row="1"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}"/>
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<Grid>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Grid.Row="0" />
<Canvas Width="128"
VerticalAlignment="Stretch"
HorizontalAlignment="Left"
Grid.Row="0"
x:Name="Image" />
</Grid>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
我知道,如果你在数据网格加载大量数据时,性能会受到影响。我可以使用虚拟化来减轻性能损失,但是,只要我把电网的自定义滚动浏览器,虚拟化都将丢失。
I know that if you load a lot of data on a data grid, the performance suffers. I can use virtualization to mitigate that performance hit, however, as soon as I throw the grid in a custom scroll viewer, the virtualization is lost.
我试图把它找回来,但我不知道如何 - 同时仍然保留指定的元素图片
在我的XAML
I am trying to get it back, but I'm not sure how -- while still retain the element named Image
in my XAML.
基本上,我想有一个形象的数据网格内容和滚动上述code正常工作,它只是我不知道如何启用虚拟化。它甚至有可能?
Basically, I want to have an image scrolling with the data grid contents and the above code works fine, it's just that I don't know how to enable virtualization. Is it even possible?
更新:看起来像我发现了一个问题。最后电网
模板中产生了一个问题:
Update: Looks like I've found a problem. The last Grid
in the template causes a problem:
<Grid>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Grid.Row="0" />
<Canvas Width="128"
VerticalAlignment="Stretch"
HorizontalAlignment="Left"
Grid.Row="0"
x:Name="Image" />
</Grid>
当我把画布
和电网
了,只留下了产品presenter
,那么它的速度快了。我怎样才能得到它速度快,仍保留此画布
?
As soon as I take the Canvas
and Grid
out, leaving only the ItemsPresenter
, then it's fast again. How can I get it fast and still retain this Canvas
?
更新2:如何申请这个(的ScrollViewer性能比较慢用的DataGrid )策略我的电网
上面显示?我想这样的:
Update 2: How can I apply this (ScrollViewer slow perfomance with DataGrid) strategy for my Grid
shown above? I tried this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle Name="sizingElement" Grid.Row="0" Fill="Transparent" Margin="1"/>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Grid.Row="0"
Height="{Binding ElementName=sizingElement, Path=ActualHeight, FallbackValue=1}" />
<Canvas Width="128"
VerticalAlignment="Stretch"
HorizontalAlignment="Left"
Grid.Row="0"
x:Name="Image" />
</Grid>
不过,现在的滚动条都消失了?
However, now the scrollbars have disappeared?
我知道我不能虚拟化一个画布
,我也不需要。事实上,整个画布
被抽,我也没有逻辑,将其分成更小的部分。这是完全没问题渲染图像的整体,只要我能保持连续的虚拟化。
I realize that I can't virtualize a Canvas
, and I don't need to. In fact, the whole Canvas
gets drawn and I have no logic to separate it into smaller parts. It's completely fine to render the image at its entirety, as long as I can keep row virtualization.
我已经得到了虚拟化的工作为我的基础上的TreeView自定义控件(.NET 4.0)。我修改了一点风格相匹配的DataGrid,希望这将会对您的工作情况:
I've got virtualization working for my custom control based on TreeView (.Net 4.0). I modified a little the style to match DataGrid, hope it is going to work for your case:
<Style TargetType="{x:Type DataGrid}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True" VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border x:Name="Border" Grid.Column="0" Background="{TemplateBinding Background}"
BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2">
<DockPanel>
<ScrollViewer x:Name="PART_Body_Scroll" Background="White" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<ItemsPresenter x:Name="ItemsHost" VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ScrollViewer>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>