我有一个非常慢的ItemsControl,其中包含最多5个DataGrids。 (显示彼此相邻)每当我更新此控件时,我的整个UI都会落后(~10秒)。每个Datagrid有大约50-100个项目,有4个预定义列(不自动生成)。网格的项目源同时更新。
这是我的XAML:
<ScrollViewer Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
<ItemsControl x:Name="icjudges" ItemsSource="{Binding Judges}" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock Text="{Binding Fullname}" Margin="4" />
</StackPanel>
<DataGrid Grid.Row="1" Margin="4,0" ScrollViewer.CanContentScroll="False" ItemsSource="{Binding}" SelectionUnit="FullRow" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" AutoGenerateColumns="False" SelectedIndex="0" AlternationCount="2" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True">
<DataGrid.DataContext>
<CollectionViewSource>
<CollectionViewSource.Source>
<MultiBinding Converter="{StaticResource selectedtrickrankingconv}">
<Binding Path="TrickRankings" />
<Binding Path="DataContext.CurrentRunEntry.SelectedTrick" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
</MultiBinding>
</CollectionViewSource.Source>
</CollectionViewSource>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="Rank" Binding="{Binding Rank}" HeaderStyle="{StaticResource centerheaderstyle}" MinWidth="20" CellStyle="{StaticResource centercellstyle}" />
<DataGridTextColumn Header="Name" Binding="{Binding Trick.Entry.Athlete.NameShort}" HeaderStyle="{StaticResource leftheaderstyle}" MinWidth="100" />
<DataGridTextColumn Header="Run" Binding="{Binding Trick.Entry.Run.Number}" HeaderStyle="{StaticResource centerheaderstyle}" MinWidth="20" CellStyle="{StaticResource centercellstyle}" />
<DataGridTextColumn Header="Score" Binding="{Binding ValueFormatted}" HeaderStyle="{StaticResource rightheaderstyle}" MinWidth="30" CellStyle="{StaticResource rightcellstyle}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
任何想法如何加快渲染过程?当我更新datagrids时,我的UI保持响应对我来说非常重要。 我也尝试用ListViews替换Datagrids,但它几乎和以前一样慢。
我想我发现了这个问题,这里是提供数据的转换器:
public class SelectedTrickJudgeScoreRankingsConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Count() != 2)
return null;
if (values.Any(x => x == DependencyProperty.UnsetValue || x == null))
return null;
IEnumerable<JudgeRanking> Rankings = (IEnumerable<JudgeRanking>) values[0];
Trick CurrentTrick = (Trick)values[1];
return Rankings.FirstOrDefault(t => t.Trick.Number == CurrentTrick.Number).ScoresSorted;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public IEnumerable<Score> ScoresSorted
{
get { return Scores.Where(s => s.IsValid).OrderBy(s => s.Rank); }
}
public bool IsValid
{
get
{
if (Value == null)
return false;
if (Value.Value >= (decimal)Trick.Entry.ContestPart.MinScore && Value.Value <= (decimal)Trick.Entry.ContestPart.MaxScore)
return true;
return false;
}
}
如果我返回ScoresSorted它非常慢,如果我只返回得分(未排序)它的速度更快,但我不知道要改变什么,因为我需要它排序
我编写了一个小型测试应用程序来演示UI的滞后, 您可以在这里下载。 https://1drv.ms/u/s!AreXFr2kgVXYjacFH0-MZsnOARPLEA
KR Manu
答案 0 :(得分:2)
我也被这种行为困住了。但我找到了一个解决办法,将DataGrids
替换为ListBox
,并将ItemsPanel
重新定义为:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
之前,只需创建一个包含所需列数的网格(以模拟标题)。
它适用于我。