WPF DataGrid闪烁问题 [英] WPF DataGrid flickering issue

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

问题描述

在WPF中,我有一个 DataGrid 绑定到 ViewModel <中的 DataView / code>类。这是一个触摸面板嵌入式项目,因此每次我在面板上触摸并滑动手指时,它都会不断向我发送值,并将这些值保存到 DataTable 并将其分配给绑定 DataView 的方式是,随着数据的更新,它只能显示5或10行(预定义)。每次获得新行时,我都会删除最后一行,直到 DataTable 行总数为5/10。

In WPF, I have a DataGrid bound to a DataView in the ViewModel class. It is a touch panel embedded project, so every time I touch and slide my fingers on the panel it keeps sending me values and saves those values to the DataTable and assigns it to the bound DataView in a way where it can show only 5 or 10 rows (predefined) as the data gets updated. Every time I get a new row I delete the last until the total DataTable row count is 5/10.

这里的问题是 DataGrid 绑定到最后一个值所花费的时间比平时更长。从面板上松开手的那一刻,我希望网格绑定到这些值,但是执行此操作大约需要6到10秒钟,用户必须等待它完成。

The issue here is that it is taking longer than usual for the DataGrid to bind to the last values. The moment I lift my hand off the panel, I expect the grid to bind to those values, but it is taking some 6 to 10 seconds to do this operation where the user has to wait for it to complete.

为此,我使用了 XAML 中的 IsAsync 属性。正如预期的那样,这会快速绑定 DataGrid ,但每次更新数据时, DataGrid 都会闪烁。如何克服 DataGrid 中的闪烁问题?还是有其他方法可以做到这一点。我们正在遵循用于绑定的MVVM模式。

For this, I have used the IsAsync property in XAML. As expected, this makes the DataGrid bind fast but every time the data gets updated, the DataGrid flickers. How can I overcome the flickering issue in the DataGrid? Or is there any other way to do it. We are following MVVM patterns for the binding.

XAML:

<DataGrid
    ItemsSource="{Binding Path=View, IsAsync=True, UpdateSourceTrigger=PropertyChanged}"
    CanUserSortColumns="False"  
    CanUserResizeRows="False"
    HorizontalScrollBarVisibility="Hidden" 
    VerticalScrollBarVisibility="Hidden"
    EnableColumnVirtualization="True" 
    EnableRowVirtualization="True"
    CanUserAddRows="False">

</DataGrid>

查看模型:

private void InsertCollection(List<string> values)
{
    if (values == null || !values.Any()) return;

    try
    {
        var dataRow = ObjectMessageDataTable.NewRow();
        int columnCount = ObjectMessageDataTable.Columns.Count;

        var inputs = values.Select(y => y as object).ToArray();
        for (var i = 0; i < inputs.Count() && i < columnCount; i++)
            dataRow[i] = values[i];

        ObjectMessageDataTable.Rows.InsertAt(dataRow, 0);
        var count = ObjectMessageDataTable.Rows.Count;

        while (count > NumOfRows)
        {
            ObjectMessageDataTable.Rows.RemoveAt(NumOfRows);
            count = ObjectMessageDataTable.Rows.Count;
        }

        ObjectMessageDataTable.AcceptChanges();
        View = ObjectMessageDataTable.DefaultView;
    }
    catch (Exception ex)
    {

    }
}


推荐答案

我尝试了您的代码。直接对数据表进行数据绑定是如此简单,并且闪烁将消失:)。只需使用ObjectMessageDataTable.DefaultView而不是View。因为每次创建View时都会以某种方式创建View对象本身,这会导致数据网格从头开始初始化。

I tried your code. It is so simple to databind the datatable directly and the flickering will go away :). just use ObjectMessageDataTable.DefaultView instead of View. Because somehow the View object itself is created everytime View is created and this causes the datagrid to initialise from the beginning.

 ItemsSource="{Binding Path=ObjectMessageDataTable.DefaultView, IsAsync=True, UpdateSourceTrigger=PropertyChanged}"

所以更改为

<DataGrid
        Grid.Row="1"
        Width="Auto"
        Height="Auto"
        Margin="0,20,0,0"
        HorizontalAlignment="Center"
        VerticalAlignment="Top"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        AlternatingRowBackground="WhiteSmoke"
        AlternationCount="2"
        AutoGenerateColumns="True"
        BorderBrush="Black"
        BorderThickness="1"
        CanUserAddRows="False"
        CanUserResizeRows="False"
        CanUserSortColumns="False"
        ColumnHeaderHeight="35"
        EnableColumnVirtualization="True"
        EnableRowVirtualization="True"
        HorizontalScrollBarVisibility="Hidden"
        ItemsSource="{Binding Path=ObjectMessageDataTable.DefaultView, IsAsync=True, UpdateSourceTrigger=PropertyChanged}"
        RowHeight="30"
        VerticalScrollBarVisibility="Hidden" />

希望有帮助。

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

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