如何只允许在 WPF 窗口中统一调整大小? [英] How Can I Only Allow Uniform Resizing in a WPF Window?

查看:22
本文介绍了如何只允许在 WPF 窗口中统一调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望仅水平"或仅垂直"调整窗口大小.是否有我可以在我的窗口上设置的属性可以强制执行此操作,或者是否有我可以使用的漂亮的代码隐藏技巧?

I don't want my window to be resized either "only horizontally" or "only vertically." Is there a property I can set on my window that can enforce this, or is there a nifty code-behind trick I can use?

推荐答案

您可以使用 WPF 的 ViewBox 保留内容的纵横比,并在内部具有固定宽度和高度的控件.

You can reserve aspect ratio of contents using WPF's ViewBox with control with fixed width and height inside.

让我们试一试.您可以更改 ViewBox 的Stretch"属性以体验不同的效果.

Let's give this a try. You can change "Stretch" attribute of ViewBox to experience different results.

这是我的屏幕截图:

<Window x:Class="TestWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Viewbox Stretch="Uniform">
        <StackPanel Background="Azure" Height="400" Width="300" Name="stackPanel1" VerticalAlignment="Top">
            <Button Name="testBtn" Width="200" Height="50">
                <TextBlock>Test</TextBlock>
            </Button>
        </StackPanel>
    </Viewbox>

</Window>

这篇关于如何只允许在 WPF 窗口中统一调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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