我通过设置属性使用Ui虚拟化的虚拟化Treeview 在TreeStyle中并在ItemsPanelTemplate中使用。树视图变得虚拟化并且能够滚动,但是获得异常:" System.ArgumentException:Width和Height必须是非负的。"将子项添加到treeview中。
我使用的treeviewStyle和TreeViewItem stlye放在下面:
<Style x:Key="TreeViewStyle" TargetType="TreeView">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Standard" />
<!-- TreeView ItemsPresenter StackPanel VirtualizingStackPanel -->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel Height="366" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<Border Name="Border"
Background="#232323"
BorderThickness="1"
CornerRadius="1">
<ScrollViewer Name="_tv_scrollviewer_"
CanContentScroll="True"
Focusable="False"
Padding="5,1,0,2"
Template="{StaticResource ScrollViewerControlTemplate3}">
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Background" Value="Red" />
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
<!-- Placing items in the tree view -->
<Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type
ItemsControl}}}" />
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type
ItemsControl}}}" />
<Setter Property="Padding" Value="1,0,0,0" />
<Setter Property="Margin" Value="-24,0,0,0" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="19" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border x:Name="Bd"
Grid.Column="1"
Margin="0,0,0,0"
BorderBrush="#505050"
BorderThickness="0,0,0,0"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<!-- Header/Root Elements -->
<ContentPresenter x:Name="PART_Header"
Margin="2,0,0,0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
ContentSource="Header"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<!-- Child Elements -->
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="0,0,0,0" />
</Grid>
<ControlTemplate.Triggers>
<!--<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我正在使用.Net版本4.5 VS2010。