0

I have a listBox, which I am trying to bind to an IList collection using ItemsSource. My problem scenario comes when each of my person object has a FlowDocument which I am trying to display in a richTextBox within the listBoxItem.

Imagine the performance degradation, when there are 1000 person objects,

Is there a way, I get to dynamically load the flowDocument / RichTextbox so that there is no performance impact.

Is there a way, I get to know which Items of the listbox are visible at any moment in time so that, I can dynamically bind the richtextbox with the flow document and when the scroll happens, I can clear the previous binding and apply binding to only those items that are visible.

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

thanks

public class Person
{
  public FlowDocument PersonHistory{get;set}
}
2
  • Please post code you have tried so far so others can help Commented Aug 25, 2016 at 11:14
  • @UmairFarooq this is the closest I could key in over here, 150 flow documents bound to the listbox, would end up causing performance hit while scrolling. Commented Aug 25, 2016 at 11:45

1 Answer 1

1

You can separate the UI in two controls to improve the performance. Consider adding a unique attribute in person class like primary key in a database table.

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

Now you can have one ListBox

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

With which you bind the PersonsCollection and set DisplayMemberPath="ID" to show only ids in ListBox.

And you have a RichTextBox separately in your xaml.

<RichTextBox Name="personHistoryTextBox"/>

If you see I have added an event with ListBox as well. The SelectionChanged Event.

In your event you can do something like this.

private void personsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
          if(PersonsListBox.SelectedItem != null){
              personHistoryTextBox.Document = (PersonsListBox.SelectedItem as Person).PersonHistory;
          } 
}
Sign up to request clarification or add additional context in comments.

8 Comments

I do like the idea of putting a selectionChangedEvent, but, what if I end up scrolling using a mouse, over there, I do not encounter a selectionChangedEvent.
You don't want to have bad performance, then make things event driven bot behavior driven. Like scrolling. Because this kind of event (if exist) changes rapidly and it will give bad performance if you do heavy tasks in this kind of event.
but is there a way out with dynamic binding and scrolling, I was wondering about that, but me check the performance with selection event, thanks.
I dont mind a second of delay in loading the document, but delay in scrolling is what I do not want!
Check with this solution. I don't think that there will be a delay with this solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.