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.开发者_如何学C 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.
Just a few things you might want to keep in mind:
- ListBoxes are a lot more heavy than StackPanels alone, ListBoxes normally contain a StackPanel themselves.
- 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.
- If you only want text then use TextBlock which is more lightweight than Label. (TextBlock vs. Label)
- How to create vertical text
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
.
精彩评论