【问题标题】:C# WPF Databinding DataGrid SlowC# WPF 数据绑定 DataGrid 慢
【发布时间】:2017-07-30 23:14:49
【问题描述】:

我的以下代码存在性能问题。我正在尝试将两个列表与两个 DataGrid 绑定。每个列表的 DataGrid。我需要一个双向绑定。有谁知道为什么?或者有什么建议?

这是我的课

    public class Vertices
{
    public int ID { get; set; }
    public double PositionX { get; set; }
    public double PositionY { get; set; }
    public string Description { get; set; }
    public bool IsMap { get; set; }
    public bool IsStartEndPoint { get; set; }
    public bool IsDisplay { get; set; }
    public bool IsExit { get; set; }
    public string TextToSpeech { get; set; }

}

public class Edges
{
    public int ID { get; set; }
    public int SourceVertex { get; set; }
    public int TargetVertex { get; set; }
    public bool IsNormal { get; set; }
    public bool IsElevatorUp { get; set; }
    public bool IsElevatorDown { get; set; }
    public bool IsStairUp { get; set; }
    public bool IsStairDown { get; set; }
}

这是我的 XAML 代码

 <DataGrid x:Name="DataGridVertices" Grid.Row="1" Grid.Column="3" BorderBrush="AliceBlue" BorderThickness="5" AutoGenerateColumns="True" ItemsSource="{Binding AllVerticesList}"  />
    <DataGrid x:Name="DataGridEdges" Grid.Row="2" Grid.Column="3" BorderBrush="AliceBlue" BorderThickness="5" AutoGenerateColumns="True" ItemsSource="{Binding AllEdgesList}"  />

这是我的代码隐藏

            this.DataGridVertices.ItemsSource = AllVerticesList;
        this.DataGridEdges.ItemsSource = AllEdgesList;

我正在使用 DataGrid.Items.Refresh() 更新 DataGrid;

谢谢!

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    不要用列表绑定数据网格,而是用 ObservableCollection 绑定它们。 并将绑定模式设置为 TwoWay 并将 UpdateSourceTrigger 设置为 PropertyChanged 。现在,您不需要刷新Datagrid,也不需要在后面的代码中设置itemsource。只需添加,删除更新集合。它将提高性能。

    如果您使用绑定,请不要使用代码隐藏。或者通过代码隐藏管理一切,不要使用绑定。两者都使用会影响性能。希望这会有所帮助。

    【讨论】:

    • 并确保您正确虚拟化项目msdn.microsoft.com/en-us/library/…
    • 并确保你已经实现了一个自定义的 ObservableCollection,它具有完美的添加范围,可以只触发一个事件,而不是每个新元素触发一个事件。
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2011-05-06
    • 2012-08-17
    • 2011-06-15
    • 2011-03-22
    • 2017-05-02
    相关资源
    最近更新 更多