WPF - 向左扩展窗口 [英] WPF - Expand Window to the Left

查看:69
本文介绍了WPF - 向左扩展窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可扩展面板的 WPF 窗口(通过 Expander).面板位于窗口的左侧,展开时窗口会增大以适应内容.

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.

我尝试在 Window.SizeChanged 事件中执行以下操作:

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.

推荐答案

我使用一个简单的解决方案设法克服了这个问题:Hide &显示.

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

代码如下:

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();
}

我用 FrameworkElement.OnRenderSizeChanged 的覆盖替换了 Window.SizeChanged 的事件处理程序.

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

这篇关于WPF - 向左扩展窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆