视图之间缓慢切换(视图包含带有许多项的数据网格)

时间:2011-08-09 19:48:07

标签: c# wpf datagrid caliburn.micro performance

我使用Caliburn Micro&基于.NET 4.0的WPF应用程序中的Fluent功能区。我的问题是视图之间的变化太慢了。我想我知道这个问题的根源。

但我不怎么解决。首先,我描述了我的应用程序的设计。

Shell是WPF窗口,它包含带有RibbonTabItem项的Fluent Ribbon.Menu。如果用户单击shell中的选项卡项,则会加载新视图。

以下是XAML的观点:

    <StackPanel Grid.Row="0">
        <Fluent:Ribbon>
            <Fluent:Ribbon.Menu>

                <Fluent:Backstage>
                    <Fluent:BackstageTabControl>
                        <Fluent:BackstageTabItem Header="Open"/>
                        <Fluent:BackstageTabItem Header="Close"/>
                    </Fluent:BackstageTabControl>
                </Fluent:Backstage>

            </Fluent:Ribbon.Menu>

            <!--Tabs-->
            <Fluent:RibbonTabItem Micro:Message.Attach="[PreviewMouseLeftButtonDown]=[Action ShowView1()]"/>
    <Fluent:RibbonTabItem Micro:Message.Attach="[PreviewMouseLeftButtonDown]=[Action ShowView2()]"/>
    <Fluent:RibbonTabItem Micro:Message.Attach="[PreviewMouseLeftButtonDown]=[Action ShowViewN()]"/>

        </Fluent:Ribbon>

    </StackPanel>

    <Grid Grid.Row="1">
        <ContentControl x:Name="ActiveItem" />
    </Grid>
</Grid>

ShellView模型类:

namespace T_TOOL.ViewModels
{

    public interface IShellViewModel{}

    [Export(typeof(IShellViewModel))]
    public class ShellViewModel :Conductor<IScreen>.Collection.OneActive,
        IShellViewModel, 
        IPartImportsSatisfiedNotification
    {

        public void ShowView1()
        {
            var screen = IoC.Get<IShowView1();
            ActivateItem(screen);
        }

        public void ShowView2()
        {
            var screen = IoC.Get<IShowView2();
            ActivateItem(screen);
        }

//...
        public void ShowViewN()
        {
            var screen = IoC.Get<IShowViewN();
            ActivateItem(screen);
        }

    }
}

ViewModel1,ViewModel2,... ViewModelN仅包含DataGrid控件。在Datagrid控件上,我从视图模型绑定ICollectionView的属性类型。

此属性包含18 000 - 25 000项。我认为这是问题的根源,为什么视图之间的变化很慢。我使用Extended WPF Toolkit中的DataGrid Control。

我将DataGrid的EnableRowVirtualization和EnableColumnVirtualization属性设置为true。但它没有帮助。

以下是ViewModel1的View XAML代码:

    <Style x:Key="MainView_CallsDataGrid" TargetType="{x:Type Controls:DataGrid}" 
           BasedOn="{StaticResource MainView_FontBaseStyle}">
        <Setter Property="AutoGenerateColumns" Value="False"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Visible"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="Margin" Value="4,15,4,15"/>
        <Setter Property="EnableRowVirtualization" Value="True"/>
        <Setter Property= "EnableColumnVirtualization" Value="True"/>
    </Style>

        <Controls:DataGrid.Columns>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=Number}"
                                         Header="Cell phone No"/>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=CallType}"
                                         Header="Call type"/>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=Dt}"
                                         Header="Date / Time"/>


            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=CallingNumber}"
                                         Header="Calling Number"/>


            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=VoiceNetwork}"
                                         Header="Voice network"/>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=Type}"
                                         Header="Type"/>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=TalkTime}"
                                         Header="Talk time"/>

            <Controls:DataGridTextColumn IsReadOnly="True"
                                         CellStyle="{StaticResource MainView_CallsDataGrid_CellStyle}"
                                         Binding="{Binding Path=Price}"
                                         Header="Price"/>

        </Controls:DataGrid.Columns>

来自ViewModel1类的

代码:

        public ICollectionView CallsView
        {
            get
            {
                return _callsView;
            }
            set
            {
                _callsView = value;
                NotifyOfPropertyChange(() => CallsView);
            }
        }

//... Init CallsView property from List<T> property

       CallsView = CollectionViewSource.GetDefaultView(List<T>);
       FilterCalls();
       CallsView.Refresh();

//Filter method
        private void FilterCalls()
        {
            if (CallsView != null)
            {
                CallsView.Filter = new Predicate<object>(FilterOut);
                CallsView.Refresh();
            }
        }

我的意见是对的?切换视图的速度很慢,因为datagrid包含很多行?或问题是我绑定DataGrid控件属性类型的ICollectionView?

感谢您的意见,建议和反馈。

这是screen shot

Screen Shot http://i51.tinypic.com/2mzyzh4.jpg

1 个答案:

答案 0 :(得分:0)

如果功能区选项卡与TabControl选项卡相同,我不肯定,但TabControl在它们不可见时会将其命名为TabItems。这会导致在切换到选项卡时重新绘制每个TabItem,并且具有大量控件的TabItem将花费大量时间来重新绘制。

我在使用TabControl时遇到了类似的问题,切换选项卡会重新绘制整个选项卡,导致切换非常慢。我最终使用显示here的代码扩展了TabControl,并防止它在切换标签时破坏了它的子项。也许你可以做一些类似于功能区选项卡

的事情