【问题标题】:Scale usercontrol in wpf?在 wpf 中缩放用户控件?
【发布时间】:2011-05-05 18:09:53
【问题描述】:

我的用户控件中有多个项目。我把所有东西都放在一个网格里。但现在我正在尝试,如果我的屏幕分辨率发生变化,窗口会自动缩放。这只是行不通。 我已经使用了 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>

——请人帮忙。

【问题讨论】:

  • 您能否直观地描述一下您希望它如何扩展?

标签: wpf scale items


【解决方案1】:

将 Grid 设置为固定的 Width 和 Height,并将 ViewBox.Stretch 设置为 Uniform。应该这样做。

【讨论】:

  • 我这样做了。但是如果我改变屏幕的分辨率(使用投影仪时),网格不会填满整个屏幕......
  • @Ruben:Beamer 不是你应该在发明它的语言之外使用的词(这是一种愚蠢的英国主义),“正确”的词应该是“投影仪”。
  • @Ruben:这很奇怪.. 但是窗口仍然以新的分辨率最大化(并且不重叠多个屏幕)? (顺便说一句,我也会使用“beamer”。不过,次要语言)
【解决方案2】:

正确的答案可能比您寻求的解决方案更复杂,但我会尽量保持简短。

要做你想做的事,根据我的经验,你最好的办法是使用一个网格作为你的主要初始元素,然后将你的控件放置在该网格(或其中的其他网格)中,并将各个控件包装在 ViewBoxes 中。之后,将 UserControl SizeChanged 事件与强制 UserControl 的高度和宽度保持适当比例的方法联系起来。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 2011-07-13
    • 2011-12-10
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多