Jaska
Jaska

Reputation: 1430

Is WPF StackPanel freezable

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>

Upvotes: 0

Views: 626

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

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

Upvotes: 1

Related Questions