【问题标题】:WPF auto resize elementsWPF 自动调整元素大小
【发布时间】:2011-09-08 17:23:06
【问题描述】:

当应用程序窗口调整大小时,我希望其中的元素也按比例调整大小。 那可能吗?我尝试使用谷歌搜索,但找不到与此相关的任何内容。 我的 XAML 代码:

<Window x:Class="app.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown">
    <Grid>
        <TabControl Name="MyTabControl">
            <TabItem Name="pradzia" Header="Pradžia" IsSelected="True">
                <Button Content="Pradžia" Height="55" Name="button1" Width="125" Click="button1_Click" />
            </TabItem>
            <TabItem Header="Vykdyti" Name="vykdyti" IsEnabled="False">
                    <Grid Button.Click="Grid_Click">
                    <Button Content="1" Height="50" HorizontalAlignment="Left" Margin="6,0,0,6" Name="button2" VerticalAlignment="Bottom" Width="125" FontSize="20" />
                        <Button Content="2" Height="50" HorizontalAlignment="Center" Name="button3" VerticalAlignment="Bottom" Width="125" FontSize="20" Margin="184,0,184,6" />
                    <Button Content="3" Height="50" HorizontalAlignment="Right" Margin="362,0,0,6" Name="button4" VerticalAlignment="Bottom" Width="125" FontSize="20" />
                        <TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Margin="0,15,0,0" Text="Kiek gyvūnų paveiksliuke?" VerticalAlignment="Top" FontSize="16" />
                    <Image Height="150" HorizontalAlignment="Center" Margin="0,-25,0,0" Name="mainImage" Stretch="Uniform" VerticalAlignment="Center" Width="200" />
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml layout window


    【解决方案1】:

    您似乎在使用网格而没有实际利用其主要功能:行和列
    使用它们,您可以通过定义星形大小的行和列来使布局按比例分配空间。

    例如

    <Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <!-- Note that width and height are not set to make it size to the grid cell -->
        <Button Grid.Row="1" Grid.Column="1" Content="Lorem Ipsum"/>
      </Grid>
    </Window>
    

    如果按比例调整大小实际上是指大小而不是分配的布局空间,则使用Viewbox

    【讨论】:

    • 好吧,实际上主要问题是,比如说,用户最大化窗口并且按钮应该调整大小以匹配比例。我想我会看看那些列和行。
    【解决方案2】:

    您希望调整大小的任何控件(例如按钮)都不应在 XAML 中定义宽度或高度。如果他们需要最小尺寸,请使用 MinWidth 和 MinHeight

    【讨论】:

    • 我不知道,这对我不起作用。控件仍未调整大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多