2017-07-30 79 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