【问题标题】:WPF Grid Row Height = Column WidthWPF 网格行高 = 列宽
【发布时间】:2018-09-03 01:30:03
【问题描述】:

我是 WPF 新手,我正在尝试执行以下操作:

  • 我想将Column0 的宽度设置为等于Row0 的高度。
  • 所有列的宽度应相同。
  • 网格的高度/宽度不应恒定。

但网格高度与网格宽度不同。 这是我的 WPF 网格代码:

    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Column0" Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition x:Name="Row0" Height="{Binding ElementName=grid,
            Path=ColumnDefinitions[0].Width}" />
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您可以使用位于网格中第 0 行/第 0 列的控件来管理大小:

    <Grid x:Name="LeGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <Label Grid.Row="0"
               Grid.Column="0"
               Width="{Binding ElementName=LeGrid, Path=ColumnDefinitions[0].Width}"
               Height="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"
               Content="First" />
    
    </Grid>
    

    【讨论】:

    • 成功了。您能告诉我如何设置窗口属性,使用户只能从右下角调整表单大小,并且表单高度与 iobit 卸载程序一样与表单宽度成比例增加。
    • 你知道这是另一个问题吗?无论如何。停止边缘调整大小的最简单方法是设置 windowstyle=none。不过,您可能不喜欢丢失 chrome。如果您希望保留标题等,则涉及更多。要保留比例,您可以处理窗口的 SizeChanged 事件。看看它是如何改变的,并设置高度/宽度以符合您的要求。
    【解决方案2】:

    我通常做这样的事情的方式是使用一个矩形。我将填充设置为透明,这里是蓝色的,所以你可以看到它在工作。

    <Grid Name="grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="Column0" Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="{Binding ElementName=measurer, Path=ActualWidth}" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Rectangle Fill="Blue" Name="measurer"/>
    </Grid>
    

    【讨论】:

    • 它有效,我认为这是迄今为止最好的解决方案。您能告诉我如何设置窗口属性,使用户只能从右下角调整表单大小,并且表单高度与 iobit 卸载程序一样与表单宽度成比例增加。
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2012-04-01
    • 2010-09-22
    • 2012-01-01
    • 2012-09-07
    • 2011-07-06
    • 2018-01-31
    相关资源
    最近更新 更多