在WPF中如何缩放用户控件?

3

我在用户控件中有多个项目。我把所有东西都放在网格中。但是现在我正在尝试,如果我的屏幕分辨率改变,窗口会自动缩放。但这并没有起作用。 我已经使用了ViewBox,但没有得到想要的结果。 这是我的用户控件:

<UserControl x:Class="NewWPFVragenBeheer.Maak_toets.Views.ChangeCourse"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:converters="clr-namespace:NewWPFVragenBeheer.Converters"
                        mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="700"
            >

    <UserControl.Resources>
        <XmlDataProvider x:Key="Vakken"
                Source="C:\Users\Ruben\Desktop\Stage 26-04\stage_TFW\stage_TFW\NewWPFVragenBeheer\Data\Courses.xml"
                XPath="/Courses/Course"
           />

        <converters:RadioBoolToIntConverter x:Key="radioBoolToIntConverter" />
    </UserControl.Resources>

    <Viewbox Stretch="None">
        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="722*" />
                <ColumnDefinition Width="254*" />
            </Grid.ColumnDefinitions>


            <Label Content="Maximale tijd:" Height="28" FontWeight="Bold" HorizontalAlignment="Left" Margin="12,28,0,0" Name="label1" VerticalAlignment="Top" Width="177"  />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="215,30,0,0" Text="{Binding Path=MaxTime}" VerticalAlignment="Top" Width="145" />

            <TextBox Height="23" HorizontalAlignment="Left" Margin="215,2,0,0" Text="{Binding Path=ExamName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="textBox1"   VerticalAlignment="Top" Width="145" />
            <Label FontWeight="Bold" Content="Punten:" Height="28" HorizontalAlignment="Left" Margin="386,0,0,0" Name="label2" VerticalAlignment="Top" />
            <TextBox Height="23"  Text="{Binding Path=Score,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  HorizontalAlignment="Left" Margin="567,2,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
            <Label Content="{Binding Path= FeedbackText, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Height="28" HorizontalAlignment="Right" Margin="0,153,527,0" Name="label3" VerticalAlignment="Top" Width="200" Foreground="#FFF50D0D" />

        </Grid>
    </Viewbox>
</UserControl>

这个用户控件设置在一个窗口中:

<Window x:Class="NewWPFVragenBeheer.MaakToetsDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:view="clr-namespace:NewWPFVragenBeheer.Views"
        Title="MaakToetsDialog" 
        WindowStyle ="SingleBorderWindow"
        WindowState ="Maximized"
        WindowStartupLocation="CenterScreen"        
       >


    <view:MaakToetsView />
</Window>

¨please someone help.


你能用图示的方式描述一下你希望它如何扩展吗? - Joel B Fant
2个回答

1

将网格设置为固定的宽度和高度,并将ViewBox.Stretch设置为Uniform。就可以了。


我已经做了这个。但是,如果我更改屏幕的分辨率(使用投影仪时),网格不会填满整个屏幕... - Ruben
@Ruben:Beamer不是你应该在它被发明的语言之外使用的词(这是一个愚蠢的英式用法),“正确”的词应该是“投影仪”。 - H.B.
@Ruben:很奇怪...但窗口仍然在新分辨率下最大化(并且不会重叠多个屏幕)吗?(顺便说一句,我也会使用“beamer”。虽然是第二语言) - Jens

0

正确的答案可能比你所寻求的解决方案更加复杂,但我会尽量简短地说明。

根据我的经验,要实现你想要的效果,最好的方法是将Grid作为你的主要初始元素,并在其中放置你的控件(或其他Grids),并将单个控件包装在ViewBoxes中。之后,将UserControl SizeChanged事件绑定到一个方法,强制UserControl的高度和宽度保持适当的比例。

这是我在WPF UI上处理它的最佳方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接