渲染WPF控件 [英] Rendering WPF Controls

查看:114
本文介绍了渲染WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望对你们大多数人有一个简单的问题. 3周以来
我正在开发我的第一个WPF应用程序.我正在使用Datagrid
使用ListCollectionView作为Itemsource.集合
大约有500-600件商品.每个项目约有7个属性
我在网格中显示的.
我的问题是,当我将数据加载到网格中时,我的整个GUI
冻结约1-2秒.如果我应用任何过滤器,也会发生同样的情况
进入收藏集.
对我来说,这似乎很不可思议,因为500件物品不是很多,而
对于Microsoft而言,控件的呈现时间只会增加
有很多物品> 10000,并且很多属性> 1000.

是否有可能让控件在
中呈现其内容 与主UI线程不同的线程,或者我该怎么做
冻结gui.

在此先感谢
Manu

Hi,

i hopefully have a simple question for most of you. Since 3 weeks
i am developing my first WPF application. I am using a Datagrid
with an ListCollectionView as Itemsource. The Collection
has approximatly 500-600 items. Each item has about 7 properties
which i am displaying in the grid.
My problem is when i load the data into the grid, my whole gui
freezes for about 1-2 seconds. The same happens if i apply any filter
to the Collection.
For me this seems really weird because 500 items is not very much, and
regarding to microsoft the rendering time of the control only increases
with a lot of items > 10000 and a lot of properties > 1000.

Is there any possibility to let the control render its content in a
different thread than the main UI Thread, or what can i do to avoid
freezing the gui.

Thanks in advance
Manu

推荐答案

嗨 似乎您没有使用Binding.我遇到了同样的问题,并通过使用Binding解决了该问题.

查看示例-如何在WPF中导航,分组,排序和筛选数据 [ 我程序中的列表可以包含> 500000项.
SingleVisualTemplate 是从FrameworkElement派生的自定义控件.使用DataTemplate的AFAIK可能会有所帮助.


Hi Seems that you aren''t using Binding. I have had same problem and I solved it by using Binding.

Look at the example - How to Navigate, Group, Sort and Filter Data in WPF[^]

Note: Grouping disables virtualization! This can bring huge performance issues on large data sets. So be careful when using it.(from the same source)


I had serious problem with "virtualization memory leak" when I used regular data binding. Than I used DataTemplate and all performance problems are gone. Take a look at my code sample. Also you can try to check if there are no items drawed out of screen by overriding "OnRender" method.
List in my program can hold > 500000 items.
SingleVisualTemplate is custom control derived from FrameworkElement. AFAIK using DataTemplate may help.


<Setter Property="ItemTemplate">
    <Setter.Value>
        <DataTemplate>
            <sve:SingleVisualTemplate
                Scale="{Binding Scale}"
                Gain="{Binding Gain}"
                State="{Binding State, Mode=TwoWay}"
                SingleBitView="{Binding SingleBitView, ElementName=local}"
                MouseUp="svt_MouseUp"
                MouseDown="svt_MouseDown"
                />
        </DataTemplate>
    </Setter.Value>
</Setter>



对不起,我的英语.
罗马



Sorry for my English.
Roman


这就是我的数据网格的XAML代码:

Thats my XAML code for the datagrid:

<DataGrid AutoGenerateColumns="false" CanUserAddRows="False" Grid.Row="2" VirtualizingStackPanel.VirtualizationMode="Standard" IsSynchronizedWithCurrentItem="True" Name="dgStartlists">
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Padding" Value="2"/>
            <Setter Property="BorderThickness" Value="0.5"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Header="Competitor" Width="120" Binding="{Binding Competitor}" CanUserSort="False" IsReadOnly="True"/>
        <DataGridTextColumn Header="Race" Width="110"  Binding="{Binding Race}" IsReadOnly="True" CanUserSort="False"/>
        <DataGridTextColumn Header="Run" Binding="{Binding Run}" Width="30" FontSize="12" />
        <DataGridTextColumn Header="Pos" Binding="{Binding Startposition}" Width="30" FontSize="12"/>
        <DataGridTextColumn Header="Bib" Binding="{Binding Bib}" Width="30" FontSize="12"/>
        <DataGridTextColumn Header="Heat" Binding="{Binding Heat}" Width="35" FontSize="12" />
        <DataGridCheckBoxColumn Header="Pending" Binding="{Binding Pending}" Width="60" />
        <DataGridComboBoxColumn Header="State" Width="50" ItemsSource="{Binding Source={StaticResource ResourceKey=StateEnum}}" SelectedItemBinding="{Binding State}"/>
        <DataGridTextColumn Header="Class" Binding="{Binding Classname}" Width="80" FontSize="12" />
        <DataGridTextColumn Header="Start Time" Binding="{Binding Starttime, StringFormat=H:mm:ss}" Width="80" FontSize="12" />
    </DataGrid.Columns>
</DataGrid>




那就是我的C#代码:




Thats my C# Code:

private ListCollectionView _cventries;

public dcStartlists()
{
  InitializeComponent();
}

#region Usercontrol load/unload

private void dcStartlists_Loaded(object sender, RoutedEventArgs e)
{
    if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        return;

    _cventries = new ListCollectionView(App.Competition.Entries);
    dgStartlists.ItemsSource = _cventries;
}

private void SetFilter()
{
   _cventries.Filter = obj => ((Entry)obj).Run == 1;
}



App.Competition.Entries是一个ObservableCollection,包含约500-600个项目.我正在使用ListCollectionView,因为我需要过滤我的数据.

当控件第一次加载时,我的GUI冻结了大约1-2秒,而当我调用SetFilter函数时,GUI也冻结了.我认为这没有虚拟化问题或类似问题,也许我通过设置itemsource来做错了什么?



App.Competition.Entries is an ObservableCollection with about 500-600 items. I am using the ListCollectionView because i need to filter my data.

When the control loads first time my GUI freezes for about 1-2 sec, and when I am calling the SetFilter Function the GUI also freezes. I think its no virtualisationproblem or something like that, maybe i do something wrong by setting the itemsource?


这篇关于渲染WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆