2017-07-30 77 views
-2

我在下面的代码中遇到了性能问题。我试图用两个DataGrids绑定两个列表。每个列表的DataGrid。我需要一个双向绑定。有谁知道为什么?或者有某种建议?C#WPF Databinding DataGrid Slow

这些都是我的班

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更新DataGrid中。刷新();

谢谢!

回答

1

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

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

+0

,并确保您的虚拟化项目正常https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.enablerowvirtualization(v=vs.110).aspx –

+0

,并确保你已经实现了一个自定义的ObservableCollection,它有一个完美的添加范围,以便每个新元素只触发一个事件而不是一个事件。 – pix