标签:
- <ListBox VirtualizingPanel.IsVirtualizing="True"
- VirtualizingPanel.VirtualizationMode="Recycling">
- </ListBox>
- <DataTemplate x:Key="ListBoxDataTemplate">
- <Grid Loaded="Grid_Loaded">
- <Label Content="{Binding Name}"></Label>
- </Grid>
- </DataTemplate>
- int index = 0;
- private void Grid_Loaded(object sender, RoutedEventArgs e)
- {
- Console.WriteLine(index);
- index++;
- }
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ListBox}">
- <ScrollViewer x:Name="ScrollViewer" CanContentScroll="false">
- <ItemsPresenter />
- </ScrollViewer>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- <Setter Property="ItemsPanel">
- <Setter.Value>
- <ItemsPanelTemplate >
- <VirtualizingStackPanel Orientation="Vertical"/>
- </ItemsPanelTemplate>
- </Setter.Value>
- </Setter>
- <Window x:Class="VirtualizingStackPanelDemo.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <DataTemplate x:Key="ListBoxDataTemplate">
- <Grid Loaded="Grid_Loaded">
- <Label Content="{Binding Name}"></Label>
- </Grid>
- </DataTemplate>
- <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
- <Setter Property="BorderThickness" Value="0"></Setter>
- <Setter Property="Margin" Value="0 0 5 0"></Setter>
- <Setter Property="MinWidth" Value="150"></Setter>
- <Setter Property="MaxHeight" Value="350"></Setter>
- <Setter Property="Height" Value="350"></Setter>
- <Setter Property="SnapsToDevicePixels" Value="True" />
- <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ListBox}">
- <ScrollViewer x:Name="ScrollViewer" CanContentScroll="True">
- <ItemsPresenter />
- </ScrollViewer>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- <!--<Setter Property="ItemsPanel">
- <Setter.Value>
- <ItemsPanelTemplate >
- <VirtualizingStackPanel Orientation="Vertical"/>
- </ItemsPanelTemplate>
- </Setter.Value>
- </Setter>-->
- </Style>
- </Window.Resources>
- <Grid>
- <ListBox Style="{StaticResource ListBoxStyle}" ItemTemplate="{StaticResource ListBoxDataTemplate}" x:Name="listbox"></ListBox>
- </Grid>
- </Window>
标签:
原文地址:http://www.cnblogs.com/aguan/p/5437859.html