Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Expand Window to the Left

I have a WPF window with expandable panel (via Expander). The panel is on the left side of the window, and when expanded the window grows to fit the content.

By default, windows are anchored to the top-left, so my window grows to the right. I'd like the window to grow to the left.

I tried to do the following in the Window.SizeChanged event:

private void onWindowSizeChanged(object sender, SizeChangedEventArgs e)
{
    Left -= (e.NewSize.Width - e.PreviousSize.Width)
}

and it works, but the growth is jerky, and I'd like to find a smoother solution.

like image 335
Omer Mor Avatar asked Aug 31 '10 08:08

Omer Mor


1 Answers

I managed to overcome this using a simple solution: Hide & Show.

Here's the code:

protected override void OnRenderSizeChanged(SizeChangeInfo sizeInfo)
{
    if (!sizeInfo.WidthChanged)
    {
        base.OnRenderSizeChanged(sizeInfo);
        return;
    }
    Hide();
    base.OnRenderSizeChanged(sizeInfo);
    Left -= (sizeInfo.NewSize.Width - sizeInfo.PreviousSize.Width);
    Show();
}

I replaced the event handler for Window.SizeChanged with this override of FrameworkElement.OnRenderSizeChanged.

like image 116
Omer Mor Avatar answered Sep 22 '22 22:09

Omer Mor