.net 缩放整个 WPF 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5022397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scale an entire WPF window
提问by Hank
I have a WPF application that I am going to be demoing to an audience on a large, high-resolution projector, and I am worried that the application will be too small to see from afar.
我有一个 WPF 应用程序,我将在大型高分辨率投影仪上向观众演示,我担心该应用程序太小而无法从远处看到。
Is there a simple way to make the ENTIRE application bigger (like the zoom slider in the WPF designer, that lets you zoom in?) I tried adding a layout transform to the Window in XAML, like so:
有没有一种简单的方法可以让整个应用程序变大(比如 WPF 设计器中的缩放滑块,它可以让你放大?)我尝试在 XAML 中向窗口添加布局转换,如下所示:
<Window.LayoutTransform>
<ScaleTransform ScaleX="1.5" ScaleY="1.5" CenterX=".5" CenterY=".5" />
</Window.LayoutTransform>
which makes the window look bigger in the designer, but seems to have no effect on the running application.
这使得窗口在设计器中看起来更大,但似乎对正在运行的应用程序没有影响。
I figure this should be dead simple with WPF's "resolution independence", high-tech text rendering, vector graphics, etc.
我认为使用 WPF 的“分辨率独立性”、高科技文本渲染、矢量图形等,这应该非常简单。
(I know I can use a screen zooming tool, but that's lame since it makes everything fuzzy, and always makes me dizzy when the presenter pans around the screen.)
(我知道我可以使用屏幕缩放工具,但这很蹩脚,因为它使一切变得模糊,并且当演示者在屏幕上平移时总是让我头晕目眩。)
采纳答案by JacobJ
I posted a fairly detailed example of scaling the main element in another question. Perhaps it would be of some use to you.
我在另一个问题中发布了一个相当详细的缩放主要元素的示例。或许对你有用。
回答by Hank
Just realized that putting the transform on the top-level control(a Grid, in my case), instead of on the window itself, has a similar effect to what I was looking for. The only difference is that the window size doesn't change so everything looks a little cramped, but that is easily remedied by making the window larger.
刚刚意识到将变换放在顶级控件上(Grid在我的情况下是 a ),而不是放在窗口本身上,与我正在寻找的效果类似。唯一的区别是窗口大小不会改变,所以一切看起来都有些局促,但这很容易通过扩大窗口来解决。
回答by Jeff T.
回答by David Rettenbacher
Working Solution
工作解决方案
At least in WPF .NET Core 3.1 Windowsupports SizeToContent="WidthAndHeight", it may work with older versions also as this property is supported since .NET Framework 3.0.
Combined with a fixed width/heightof the content control and a ScaleTransformset on LayoutTransform, it scales the entire window.
至少在 WPF .NET Core 3.1Window支持中SizeToContent="WidthAndHeight",它也可以与旧版本一起使用,因为自 .NET Framework 3.0 起支持此属性。
结合内容控件的固定宽度/高度和ScaleTransform设置LayoutTransform,它可以缩放整个窗口。
Recipe
食谱
- Window
SizeToContent:WidthAndHeight
- Content
- Fixed size
LayoutTransform: your customScaleTransform
- 窗户
SizeToContent:WidthAndHeight
- 内容
- 固定尺寸
LayoutTransform: 你的习惯ScaleTransform
Note
笔记
Setting SizeToContentby a style doesn't work(seems too late in the process).
SizeToContent按样式设置不起作用(在此过程中似乎为时已晚)。
Sample XAML
示例 XAML
<Window x:Class="Some.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Some"
mc:Ignorable="d"
Title="MainWindow"
SizeToContent="WidthAndHeight"
>
<Window.Resources>
<ResourceDictionary>
<ScaleTransform x:Key="windowScaleTransform" ScaleX="0.5" ScaleY="0.5" />
</ResourceDictionary>
</Window.Resources>
<Grid Width="1080"
Height="1920"
LayoutTransform="{StaticResource windowScaleTransform}"
>
<TextBlock>This window is scaled to 50%!</TextBlock>
</Frame>
</Window>

