大家好我有这种情况:
我有一个带有ItemTemplate和VirtualizingPanel的ListView作为itemsPanel,我的列表视图绑定到一个ObservableCollection如下:
//...
<ListView x:Name="MyListView" ItemsSource="{Binding Products, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource ItemContainerStyle}"> <ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtulizingPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
//...
在MainViewModel中我有这个代码来填充集合:
backworker.DoWork += (s, e) =>
{
for (int i = 0; i < 800; i++ )
{
if (!backworker.CancellationPending)
{
DispatcherHelper.UIDispatcher.BeginInvoke(new Action(() =>
{
this.Products.Add(new Product()
{
ID = i,
Name = string.Format("Product {0}", i),
Category = new Category() { Name = "Standard Category" },
SalePrice = i*50, IsActive = true
});
}));
}
else
{
break;
}
System.Threading.Thread.Sleep(500);
}
我的项目风格:
<Style x:Key="ItemContainerStyle" TargetType="{x:Type ListViewItem}" BasedOn=" {StaticResource MetroListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" Background="{TemplateBinding Background}" Margin="5" Padding="10" Width="350" Height="55" SnapsToDevicePixels="true" ToolTip="{Binding Name}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10*" MaxWidth="150" />
<ColumnDefinition />
<ColumnDefinition Width="5*" MaxWidth="150" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="PART_Background" Grid.ColumnSpan="4" Fill="Silver" Opacity=".7" Margin="-10" />
<TextBlock x:Name="PART_Icon"
Margin="2,2,10,2"
Height="25"
TextAlignment="Center"
helpers:ProductHelper.isQuantityFollowing="True"
Text="{StaticResource Product}"
FontFamily="minigcrm"
FontSize="24" />
<TextBlock x:Name="PART_Name"
Text="{Binding Name}"
TextTrimming="CharacterEllipsis"
Foreground="Black" Grid.Column="1"
FontFamily="Segoe UI"
FontSize="17"
VerticalAlignment="Center" />
<TextBlock x:Name="PART_Count"
Text="{Binding PurchasePrice, StringFormat={StaticResource (PriceDZDString)}}"
Foreground="#555"
Grid.Column="3"
FontFamily="Segoe UI Light"
FontSize="15"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
<StackPanel Orientation="Horizontal" Grid.ColumnSpan="4" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="-9">
<Button x:Name="PART_MoveToTrashOrRestor"
Style="{StaticResource FlatAndScaleButtonStyle}"
ToolTip="{StaticResource MoveToTrashString}"
Width="15"
Height="15"
Margin="2"
Command="{Binding Main.ChangeProductStatusCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Content="{StaticResource Remove}"
FontFamily="minigcrm"
FontSize="12"
Foreground="#555"
Cursor="Hand" />
<Button x:Name="PART_Delete"
Style="{StaticResource FlatAndScaleButtonStyle}"
ToolTip="{StaticResource DeleteString}"
Width="15"
Height="15"
Margin="2"
Command="{Binding Main.DeleteProductCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Content="{StaticResource Close}"
FontFamily="minigcrm"
FontSize="15"
Foreground="#555"
Cursor="Hand"
Visibility="{Binding IsActive, Converter={StaticResource InvertBooleanToVisibilityConverter}}"/>
</StackPanel>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="PART_Name" Property="Foreground" Value="WhiteSmoke" />
<Setter TargetName="PART_Count" Property="Foreground" Value="WhiteSmoke" />
<Setter TargetName="PART_Icon" Property="Foreground" Value="WhiteSmoke" />
<Setter TargetName="PART_MoveToTrashOrRestor" Property="Foreground" Value="WhiteSmoke" />
<Setter TargetName="PART_Delete" Property="Foreground" Value="WhiteSmoke" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Direction="0" ShadowDepth="0" BlurRadius="10" Color="LightGray" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_Name" Property="Foreground" Value="White" />
<Setter TargetName="PART_Count" Property="Foreground" Value="White" />
<Setter TargetName="PART_Icon" Property="Foreground" Value="White" />
<Setter TargetName="PART_MoveToTrashOrRestor" Property="Foreground" Value="WhiteSmoke" />
<Setter TargetName="PART_Delete" Property="Foreground" Value="WhiteSmoke" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Direction="0" ShadowDepth="0" BlurRadius="10" Color="LightGray" />
</Setter.Value>
</Setter>
</Trigger>
<DataTrigger Binding="{Binding IsActive, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter TargetName="PART_Background" Property="Fill" Value="Orange"/>
<Setter TargetName="PART_MoveToTrashOrRestor" Property="Content" Value="{StaticResource Restore}"/>
<Setter TargetName="PART_MoveToTrashOrRestor" Property="ToolTip" Value="{StaticResource RestoreString}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!--<DataTrigger Binding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="IsSelected" Value="False" />
</DataTrigger>-->
<DataTrigger Binding="{Binding IsActive, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Background" Value="Orange" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Active, ElementName=THIS, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False"/>
<Condition Binding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="IsSelected" Value="False" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Inactive, ElementName=THIS, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False"/>
<Condition Binding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="IsSelected" Value="False" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
我遇到的问题是在产品系列的每次更改中(当我添加新项目时)ListView在UI中重新加载导致慢速渲染的所有项目,特别是当项目数大于50项时,请求什么是问题吗?提前致谢。