2016-08-25 153 views
0

我有一個listBox,我試圖用ItemsSource綁定到IList集合。我的問題情景出現時,我的每個人對象都有一個FlowDocument,我試圖在listBoxItem中的richTextBox中顯示。動態數據綁定ListBox + WPF + FlowDocument

想象的性能下降,當有1000點人的對象,

有沒有辦法,我是那麼的沒有性能影響動態加載的FlowDocument /的RichTextbox。

有沒有辦法,我知道哪些項目的列表框在任何時刻都可見,這樣我就可以動態地將richtextbox與流程文檔綁定在一起,當滾動發生時,我可以清除上一個綁定並將綁定僅應用於可見的項目。

<ListBox ItemsSource="{Binding PersonsCollection"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <RichTextBox Document="{Binding PersonHistory}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

感謝

public class Person 
{ 
    public FlowDocument PersonHistory{get;set} 
} 
+0

請張貼到目前爲止您已經嘗試代碼,以便其他人可以幫助 –

+0

@UmairFarooq這是我能在這裏鍵最接近,綁定到列表框150個文件,最終會導致性能下降,同時滾動。 – Sandepku

回答

1

您可以用兩種控制UI分離以提高性能。考慮在人員類中添加唯一屬性,如數據庫表中的主鍵。現在

public class Person 
{ 
    public long ID{get;set;} 
    public FlowDocument PersonHistory{get;set} 
} 

你可以有一個列表框

<ListBox Name="PersonsListBox" ItemsSource="{Binding PersonsCollection"} DisplayMemberPath="ID" SelectionChanged="personsList_SelectionChanged"> 
</ListBox> 

與您綁定PersonsCollection並設置DisplayMemberPath="ID"顯示列表框中唯一的ID。

而你在你的xaml中有一個RichTextBox。

<RichTextBox Name="personHistoryTextBox"/> 

如果你看到我已經添加了一個ListBox事件。 SelectionChanged事件。

在你的活動中,你可以做這樣的事情。

private void personsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
{ 
      if(PersonsListBox.SelectedItem != null){ 
       personHistoryTextBox.Document = (PersonsListBox.SelectedItem as Person).PersonHistory; 
      } 
} 
+0

我確實喜歡將selectionChangedEvent放在一起,但是,如果我最終在使用鼠標滾動,那麼我不會遇到selectionChangedEvent。 – Sandepku

+1

你不想有不好的表現,然後讓事件驅動bot動作。像滾動一樣。因爲這種事件(如果存在的話)會迅速變化,如果您在這類事件中執行繁重的任務,它將會表現糟糕。 –

+0

但有沒有動態綁定和滾動的出路,我想知道的,但我檢查與選擇事件的表現,謝謝。 – Sandepku