我有一个具有虚拟化功能的ItemsControl。作为DataTemplate,我有一个WrapPanel。问题是当我调整容器大小时,包装面板不能正确重新绘制。它包装新行以包装文本,但文本仍保留在顶行。
我创建了一个简单的测试项目来显示问题。 这是XAML :( MainWindow.xaml)
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<Style x:Key="ItemsControlStyle"
TargetType="ItemsControl">
<Setter Property="Background"
Value="White" />
<Setter Property="ScrollViewer.CanContentScroll"
Value="True" />
<Setter Property="VirtualizingPanel.IsVirtualizing"
Value="True" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ItemsControl">
<ScrollViewer BorderThickness="0"
CanContentScroll="True"
Padding="{TemplateBinding Control.Padding}"
Background="{TemplateBinding Panel.Background}"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
Focusable="False">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ItemsControl Name="Items"
Grid.Row="1"
AlternationCount="2"
Style="{StaticResource ItemsControlStyle}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
<TextBlock Text="Wrap Item" />
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
这是代码:(MainWindow.xaml.cs)
using System.Linq;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Items.ItemsSource = Enumerable.Range( 0, 30 );
}
}
}