0
votes

I have created a window, where two vertical -oriented stack panel(left and right) are in the third, horizontal-oriented stackpanel. Left stack panel holds a number of controls and a button >Hide<, binded to command. And I dlike to animate a "hiding" right stackpanel and resize a window to the width of only left stackpanel. How to do this in the most simple way? I have found WPF stackpanel visibilty animation but , it will not resize a window.

My code is:

<Window x:Class="Namespace.MainWindow"
 ...
 x:Name="ololo"
        Height="400" Width="500">
 .....    
<StackPanel Orientation="Horizontal">
            <StackPanel x:Name="Left" Orientation="Vertical">
                <Button Margin="0,10,0,0" Command="{Binding HideCommand}">Hide</Button>
            </StackPanel>
            <StackPanel x:Name="Right" Orientation="Vertical">
            <!--<Controls>-->
            </StackPanel>
    </StackPanel>
2
Well did you try the linked XAML in a Window's Style?Kilazur
I have resource dictionary - there I store styles for textboxes, buttons and fonts - Are you asking about it?curiousity

2 Answers

2
votes

You can use the SizeToContent property on the Window to automatically resize the window when the content changes.

I would recommend the Width value.

<Window ...
    SizeToContent="Width"

However there is one gotcha, which is that you may need to specify the width of each StackPanel, or perhaps set a MinWidth on your Window to prevent the Window from resizing to a 0 width.

1
votes

Normally animation is done purely in the view, the ViewModel should not have anything to do with the size of your window. So to preserve the MVVM you will just need to adjust the size in the code behind. This normally is by looking up a resource that is the Animation.

<Storyboard x:Key="AnimateWindow">
        <DoubleAnimation Duration="0:0:3.8"
                         Storyboard.TargetProperty="Width"
                         From="350"
                         To="100"
                         AccelerationRatio=".1">
        </DoubleAnimation>
</Storyboard>

So on the button click you would also need to handle the click event so to fire off the animation. The click event will look up the resource and execute it. It would also handle any other view specific things you need.

private void Button_Click(object sender, RoutedEventArgs e)
{
        var animate= FindResource("AnimateWindow") as Storyboard;
        if (hide != null)
        {
            animate.Begin(this, true);
        }
}

This preserves the MVVM and should accomplish what you want.