渲染WPF控件

编程入门 行业动态 更新时间:2024-10-15 12:36:20
本文介绍了渲染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中导航,分组,排序和筛选数据 [ ^ ] 注意:分组会禁用虚拟化!这会给大型数据集带来巨大的性能问题.因此,使用它时要小心.(来自同一来源) 使用常规数据绑定时,虚拟化内存泄漏"存在严重问题.比起我使用DataTemplate来说,所有性能问题都消失了.看一下我的代码示例.另外,您可以尝试通过覆盖"OnRender"方法来检查是否有任何项目不在屏幕上绘制. 我程序中的列表可以包含> 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控件

本文发布于:2023-11-27 23:16:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!