在我的WPF应用程序中,我有一个TabControl
,其中有三个TabItems
。每个TabItem
都有一个ItemsControl
,其中ItemsSource
绑定到Viewmodel中的ObservableCollection
。如果我打开第一个和第二个TabItem
,则CPU使用率<10%。但是,当我打开第三个Tabitem
时,CPU使用率增加到> 70%,我不知道为什么。
我的第一个想法是,数据绑定会导致这种情况。所以我从这个TabItem
删除了任何绑定。但即使第三个TabItem
没有内容也没有调用代码,CPU使用率仍然很高。如果我切换回第一个和第二个TabItem
,CPU使用率将恢复正常。
<TabControl Grid.Row="1">
<TabItem Header="Tab1" IsSelected="True">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
<GradientStop Color="#f0f0f0" Offset="0" />
<GradientStop Color="#DBFFDF" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="5" />
<RowDefinition Height="60*" />
<RowDefinition Height="5" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="175" />
<ColumnDefinition Width="15" />
<ColumnDefinition Width="70*" />
<ColumnDefinition Width="5" />
</Grid.ColumnDefinitions>
<Border Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#F0F0F0" BorderBrush="DarkGray" BorderThickness="3">
<ScrollViewer IsDeferredScrollingEnabled="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<view:Overview />
</ScrollViewer>
</Border>
<Border Grid.Column="3" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#F0F0F0" BorderBrush="DarkGray" BorderThickness="3">
<ScrollViewer IsDeferredScrollingEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding OverviewElementCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</Border>
</Grid>
</TabItem>
<TabItem Header="Tab2">
<ScrollViewer IsDeferredScrollingEnabled="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TabControl ItemsSource="{Binding TabItemCollection}" />
</ScrollViewer>
</TabItem>
<TabItem Header="Tab3">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
<GradientStop Color="#f0f0f0" Offset="0" />
<GradientStop Color="#DBFFDF" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="5" />
<RowDefinition Height="60*" />
<RowDefinition Height="5" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="175" />
<ColumnDefinition Width="15" />
<ColumnDefinition Width="70*" />
<ColumnDefinition Width="5" />
</Grid.ColumnDefinitions>
<Border Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#F0F0F0" BorderBrush="DarkGray" BorderThickness="3">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
</ScrollViewer>
</Border>
<Border Grid.Column="3" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#F0F0F0" BorderBrush="DarkGray" BorderThickness="3">
<ScrollViewer IsDeferredScrollingEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding ErrorOverviewElementCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</Border>
</Grid>
</TabItem>
</TabControl>
所以我希望你能帮助我!