Moon Walker
Moon Walker

Reputation: 43

Fastest way to add text to canvas dynamically

I made a simple chart by putting stack panels on a canvas and then filling the stack panels with lables according to the data given.However I am not very happy with the performance.

If I change the stack panels with listboxes, and instead of lables add strings, would it be faster to render the data? The data I need to show is a subset from a large array of lists of strings. When I move the index, I have to clear all the listboxes and fill them with the new data. Or is there a faster way to do it?

All my strings are of 4 characters. Is it possible to change the orientation to vertical so that each listbox shows something like this

A

A

A

A

B

B

B

B

C

C

C

C

D

D

D

D

etc... The strings are AAAA, BBBB, CCCC, DDDD.

Upvotes: 0

Views: 2044

Answers (2)

Josh G
Josh G

Reputation: 14256

I think you would want ItemsControl, not ListBox (no need for item selection).

The question is whether the overhead is worth it for you. ItemsControl is more heavy weight than StackPanel (as H.B. said, it often contains a StackPanel). The reason to use an ItemsControl would be UI virtualization. If many of the items are scrolling off of the screen, ItemsControl will usually (not always, depends on the control template) only generate visuals for the ones that are on the screen. This saves a lot of processing time.

If you want raw speed, create a DrawingVisual, call RenderOpen() on this visual, use the DrawingContext to draw the items and then display the DrawingVisual inside of the Canvas.

Upvotes: 1

brunnerh
brunnerh

Reputation: 184416

Just a few things you might want to keep in mind:

  1. ListBoxes are a lot more heavy than StackPanels alone, ListBoxes normally contain a StackPanel themselves.
  2. ListBoxes are to be used if selection is wanted, if not you'd use an ItemsControl, But ItemsControls are composite controls and hence haevier than a container alone.
  3. If you only want text then use TextBlock which is more lightweight than Label. (TextBlock vs. Label)
  4. How to create vertical text

Upvotes: 1

Related Questions