0

I'm using StackPanel in WPF just for easy layouting horizontally aligned boxes and lines, but it seems the performance of application is getting slower when using StackPanel.

All the Microsoft tutorials seems to talk only about freezing some SolidColorBrush etc. objects, but can StackPanel be freezed after first layout so the CPU doesn't have to layout it all the time, just once?

Or am I just forced to use the very fast Canvas object and layout all the objects inside it one by one?

Example 1: Very easy to layout in designer, but lacks performance:

    <StackPanel Height="35" Canvas.Left="49" Canvas.Top="874" Width="395" Orientation="Horizontal">
        <TextBox Text="test" Width="65"/>
        <Rectangle Stroke="Black" Width="1" />
        <TextBox Text="test" Width="55"/>
        <Rectangle Stroke="Black" Width="1" />
        <TextBox Text="test" Width="75"/>
        <Rectangle Stroke="Black" Width="1" />
        <TextBox Text="test" Width="35"/>
        <Rectangle Stroke="Black" Width="1" />
        <TextBox Text="test" Width="95"/>
        <Rectangle Stroke="Black" Width="1" />
        <TextBox Text="test" Width="65"/>
        <Rectangle Stroke="Black" Width="1" />
    </StackPanel>

Example 2: Good performance, making layout in designer is a pain:

    <Canvas Height="35"  Canvas.Left="49" Canvas.Top="914" Width="395">
        <TextBox Text="test" Width="65" Height="35"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="65"/>
        <TextBox Text="test" Width="55" Canvas.Left="66" Height="35"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="195"/>
        <TextBox Text="test" Width="75" Height="35" Canvas.Left="122"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="121"/>
        <TextBox Text="test" Width="35" Height="35" Canvas.Left="198"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="197"/>
        <TextBox Text="test" Width="95" Height="35" Canvas.Left="234"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="329"/>
        <TextBox Text="test" Width="65" Height="35" Canvas.Left="330"/>
        <Rectangle Stroke="Black" Width="1" Height="35" Canvas.Left="233"/>         
    </Canvas>
Jaska
  • 1,412
  • 1
  • 18
  • 39
  • post your XAML. Also if you have large lists with much scrolling you'd be better using `VirtualizingStackPanel` instead. – Federico Berasategui Mar 08 '13 at 20:36
  • 1
    No scrolling planned for this - just laying out ~15 textboxes and 1 pixel wide black rectangles in turns side by side. All of these wrapped inside a UserControl, and then about 5-10 of these UserControls in a form. – Jaska Mar 08 '13 at 20:58
  • 1
    1 - post your XAML. 2 - Please don't call a WPF `Window` a "form". That's an insult to my beliefs and values. – Federico Berasategui Mar 08 '13 at 20:59
  • By "Form" I meant a "fillable form", u know, "an application for job interview" for example :) XAML coming up... – Jaska Mar 08 '13 at 21:04
  • I copied and pasted your first XAML into a test WPF project and it takes `00:00:00.310` to load. I don't see where the performance issue is. There must be something else. As a side note, why are you using the designer? write the XAML yourself and you'll get much better results. – Federico Berasategui Mar 08 '13 at 21:26
  • Well, actually in my real code the TextBoxes are some usercontrols which I'm not willing to give here.. but anyway, the performance issues arises when my window is rendered at 60fps (scrolling with animation), the stackpanel just can't do the layout at 60fps rate :( But you're totally missing my question here, which was "is `StackPanel` a `Freezable` object"? – Jaska Mar 08 '13 at 21:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25865/discussion-between-highcore-and-jaska) – Federico Berasategui Mar 08 '13 at 21:38

1 Answers1

1

No, StackPanel is not Freezable, however you mentioned that your GPU is having trouble rendering Animations at 60fps, Try dropping the Animation fps

In you main Windows constructor you can override the defualt framerate for Animations

I usally use 30fps as it is still smooth and users with low-end GFX cards will be able to run your application a lot smotther.

Example:

    public MainWindow() 
    {
        InitializeComponent();

        Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline)
           , new FrameworkPropertyMetadata { DefaultValue = 30 }); // 30 = 30fps
    }

Give it a try and see if it helps

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • @HighCore already directed me to the right direction - I should use rendertransformations for moving and scaling objects, not the margins and widths and heights and stuff.. So I will try with those. – Jaska Mar 08 '13 at 22:06