如何根据窗口的大小调整元素的大小

时间:2018-08-18 17:52:33

标签: c# wpf resize

我有此代码:

    <Window x:Class="App_for_stanwood.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:App_for_stanwood"
    mc:Ignorable="d"
    Title="MainWindow" Height="438.983" Width="580.615" Loaded="Window_Loaded" Icon="spartans.jpg" MinWidth="580.615" MinHeight="438.983" HorizontalAlignment="Center" VerticalAlignment="Center" SizeToContent="WidthAndHeight">
<Grid>
    <Image x:Name="image" HorizontalAlignment="Left" Width="567" Source="spartan.png" Margin="10,10,-4,0" Opacity="0.2" Height="398" VerticalAlignment="Top">
        <Image.OpacityMask>
            <ImageBrush ImageSource="spartan.png" TileMode="Tile"/>
        </Image.OpacityMask>
    </Image>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="573"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="408"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    </Grid>

    <TextBox x:Name="tbID" Margin="10,36,0,0" TextWrapping="Wrap" TextChanged="tbID_TextChanged" HorizontalAlignment="Left" Width="72" Height="23" VerticalAlignment="Top" RenderTransformOrigin="0,0">
        <TextBox.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </TextBox.RenderTransform>
    </TextBox>
    <Label x:Name="label" Content="Student ID: " HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0,0">
        <Label.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </Label.RenderTransform>
    </Label>
    <GroupBox x:Name="groupBox1" Header="Season 1" Margin="195,10,0,0" Height="199" VerticalAlignment="Top" HorizontalAlignment="Left" Width="183" RenderTransformOrigin="0.5,0">
        <StackPanel>
            <RadioButton x:Name="C" Content="Cheer" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="C_Click"/>
            <RadioButton x:Name="CC" Content="Cross Country" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="CC_Click"/>
            <RadioButton x:Name="FB" Content="Football" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="FB_Click"/>
            <RadioButton x:Name="GS" Content="Girls Soccer" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="GS_Click"/>
            <RadioButton x:Name="GSw" Content="Girls Swimming" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="GSw_Click"/>
            <RadioButton x:Name="BT" Content="Boys Tennis" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BT_Click"/>
            <RadioButton x:Name="VB" Content="Volleyball" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="VB_Click"/>
        </StackPanel>
    </GroupBox>
    <GroupBox x:Name="groupBox2" Header="Season 2" Margin="383,10,7,0" VerticalAlignment="Top" RenderTransformOrigin="1,0">
        <StackPanel Height="177" VerticalAlignment="Top" RenderTransformOrigin="1,0">
            <RadioButton x:Name="BB" Content="Boys Basketball" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BB_Click"/>
            <RadioButton x:Name="GB" Content="Girls Basketball" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="GB_Click"/>
            <RadioButton x:Name="C2" Content="Cheer" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="C2_Click"/>
            <RadioButton x:Name="BS" Content="Boys Swimming" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BS_Click"/>
            <RadioButton x:Name="W" Content="Wrestling" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="W_Click"/>
        </StackPanel>
    </GroupBox>
    <GroupBox x:Name="groupBox3" Header="Season 3" Margin="195,209,195,0" RenderTransformOrigin="0.5,1">
        <StackPanel>
            <RadioButton x:Name="BsB" Content="Baseball" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BsB_Click" />
            <RadioButton x:Name="BG" Content="Boys Golf" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BG_Click"/>
            <RadioButton x:Name="GG" Content="Girls Golf" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="GG_Click"/>
            <RadioButton x:Name="BSc" Content="Boys Soccer" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="BSc_Click"/>
            <RadioButton x:Name="SB" Content="Softball" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="SB_Click"/>
            <RadioButton x:Name="GT" Content="Girls Tennis" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="GT_Click"/>
            <RadioButton x:Name="T" Content="Track" HorizontalAlignment="Left" Margin="3,10,0,0" VerticalAlignment="Top" Click="T_Click"/>
        </StackPanel>
    </GroupBox>
    <Button x:Name="btLogin" Content="Login&#xA;" HorizontalAlignment="Left" Margin="100,39,0,0" VerticalAlignment="Top" Width="75" Height="20" Click="btLogin_Click" RenderTransformOrigin="0,0">
        <Button.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </Button.RenderTransform>
    </Button>
    <Label x:Name="lblInto" Content="" HorizontalAlignment="Left" Height="53" Margin="10,89,0,0" VerticalAlignment="Top" Width="180" RenderTransformOrigin="0,0">
        <Label.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </Label.RenderTransform>
    </Label>
    <Button x:Name="btSubmit" Content="Submit" HorizontalAlignment="Left" Margin="100,64,0,0" VerticalAlignment="Top" Width="75" Click="btSubmit_Click" RenderTransformOrigin="0,0">
        <Button.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleY="1" ScaleX="1"/>
                <SkewTransform AngleY="0" AngleX="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform/>
            </TransformGroup>
        </Button.RenderTransform>
    </Button>

</Grid>

我正在尝试使其变得可调整大小,但现在不是,它看起来很奇怪。我到处走走了很多,却没有发现任何有用的东西。我发现的所有内容都只是围绕元素移动,而不是调整它们的大小。我正在学校使用此程序注册体育运动,我需要它能够调整窗口大小。

2 个答案:

答案 0 :(得分:0)

将比例转换应用于网格

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

在此示例中,缩放比例将设置为150%。

答案 1 :(得分:0)

  

调整窗口大小时如何调整元素大小

有一个名为ViewBox的布局控件,可以自动 Transforms 元素(而不是 Resize )。但我认为这是您想要的。只需将您的内容放入其中即可:

<Window ...>
    <ViewBox ...>
        <Grid ...>
           <!-- your code here -->
        </Grid>
    </ViewBox>
</Window>