Datagrid呈现缓慢

时间:2015-09-03 10:02:28

标签: c# wpf datagrid

我正在使用Datagrid,它有大约400-500行且非常慢。我正在使用.NET 4.5并尝试使用属性

VirtualizingPanel.IsVirtualizingWhenGrouping="True"

但没有效果。有人可以帮助我,为什么这个属性不适合我吗?

我的datagrid xaml是

    <DataGrid VirtualizingPanel.IsVirtualizingWhenGrouping="True"  VirtualizingPanel.VirtualizationMode="Recycling" Name="DetailsDlg"  Grid.Column="1" Grid.Row="0" MinWidth="300" MinHeight="500"  IsReadOnly="True" CanUserSortColumns="False" CanUserDeleteRows="False" SelectionMode="Single" ItemsSource="{Binding UnitDataTable, Mode=OneWay}"/>

我尝试删除VirtualizingPanel.VirtualizationMode但没有效果。渲染花费时间不加载我的DataTable

我的ViewModel

    private object _SelectedItem;
    public object SelectedItem
    {
        get
        {
            return _SelectedItem;
        }
        set
        {
            if (value != _SelectedItem)
            {
                _SelectedItem = value;

                OnPropertyChanged("SelectedItem");
                UpdateDataTable(_SelectedItem);
            }
        }
    }

    private DataTable _UnitDataTable;
    public DataTable UnitDataTable
    {
        get
        {
            return _UnitDataTable;
        }
        set
        {
            if (value != _UnitDataTable)
            {
                _UnitDataTable = value;
                OnPropertyChanged("UnitDataTable");
            }
        }
    }

    private void UpdateDataTable(object SelectedObject)
    {
        unittable = new DataTable("Unit Table");

        if (SelectedObject != null)
        {
            string temp = ORBATTool.SelectedUnit.ToString();

            List<MUnit> tempUnitList = new List<MUnit>();

            if (SelectedObject is UnitTree)
            {                    
                AddChildUnitsToList(SelectedObject as UnitTree, tempUnitList);       
            }

            else if (SelectedObject is MUnit)
            {
                tempUnitList.Add(SelectedObject as MUnit);
            }
            List<string> resList = CreateResList(tempUnitList);



            unittable.Columns.Add(iNet_Localization.Cultures.Resources.Callsign);
            unittable.Columns.Add(iNet_Localization.Cultures.Resources.Size);
            unittable.Columns.Add(iNet_Localization.Cultures.Resources.Strength);
            unittable.Columns.Add(iNet_Localization.Cultures.Resources.Position);

            foreach (string st in resList)
            {
                string colname = st.Replace("(", "|");
                colname = colname.Replace(")", "|");
                unittable.Columns.Add(colname);
            }

            int firstRes = 4;
            int[] scoreArray = new int[resList.Count + firstRes];                

            foreach (MUnit unit in tempUnitList)
            {                   
                DataRow dr = unittable.NewRow();

                dr[0] = unit.Callsign;
                dr[1] = resourceSet.GetString(unit.Size,true);
                dr[2] = unit.Strength;
                dr[3] = unit.PositionInCurrentGSP;
                int resindex = firstRes;

                foreach (string res in resList)
                {
                    string resstring = string.Empty;
                    foreach (var unitres in unit.Resources)
                    {
                        if (unitres.Value == res)
                        {
                            resstring = unitres.Key;
                        }
                    }                        

                    // Calculate Res 'Scores'
                    try
                    {
                        if (resstring != string.Empty)
                        {
                            int numVal = Convert.ToInt32(resstring);
                            scoreArray[resindex] = scoreArray[resindex] + numVal;
                        }
                    }
                    catch (FormatException e)
                    {
                        // ignore value?
                    }
                    dr[resindex++] = resstring;
                }

                unittable.Rows.Add(dr);
            }
            UnitDataTable = unittable;
        }

    }

1 个答案:

答案 0 :(得分:0)

这些事情可以帮助提高绩效。

  • 如果您的数据网格中有大量的行和列,则应使用自定义分页。

  • 删除外部滚动查看器(如果已应用)。

    EnableColumnVirtualization = true
    EnableRowVirtualization = true
    ItemsSource="{Binding MyDataTable, IsAysnc=True}