English 中文(简体)
缩放整个WPF窗口
原标题:Scale an entire WPF window
  • 时间:2011-02-16 21:34:20
  •  标签:
  • .net
  • wpf

我有一个WPF应用程序,我将在一个大的、高分辨率的投影仪上向观众演示,我担心这个应用程序太小,无法从远处看到。

有没有一种简单的方法可以让整个应用程序变得更大(比如WPF设计器中的缩放滑块,它可以让你放大?)我尝试在XAML中向Window添加布局转换,如下所示:

<Window.LayoutTransform>
    <ScaleTransform ScaleX="1.5" ScaleY="1.5" CenterX=".5" CenterY=".5" />
</Window.LayoutTransform>

这使得窗口在设计器中看起来更大,但似乎对正在运行的应用程序没有影响。

我认为这应该是非常简单的WPF的“分辨率独立性”,高科技的文本渲染,矢量图形,等等。

(我知道我可以使用屏幕缩放工具,但这很糟糕,因为它会让一切变得模糊,而且当演讲者在屏幕上平移时,总是让我头晕。)

最佳回答

我发布了一个相当详细的缩放主元素的示例在另一个问题中。也许这对你有一些用处。

问题回答

我刚刚意识到,将转换放在顶级控件(在我的情况下是Grid)上,而不是放在窗口本身上,具有与我所寻找的类似的效果。唯一的区别是窗户的尺寸没有改变,所以一切看起来都有点局促,但通过把窗户做大很容易弥补这一点。

Using ViewBox would be the simplest way to make an entire application bigger, even the font size. Here you can find some discussion about ViewBox.

Working Solution

At least in WPF .NET Core 3.1 Window supports SizeToContent="WidthAndHeight", it may work with older versions also as this property is supported since .NET Framework 3.0.
Combined with a fixed width/height of the content control and a ScaleTransform set on LayoutTransform, it scales the entire window.

Recipe

  • Window
    • SizeToContent: WidthAndHeight
  • Content
    • Fixed size
    • LayoutTransform: your custom ScaleTransform

Note

通过样式设置SizeToContent不起作用(在此过程中似乎为时已晚)。

Sample 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>





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签