【问题标题】:WPF - Mix of Auto and Proportionate Row SizingWPF - 自动和按比例调整行大小的组合
【发布时间】:2019-05-23 07:26:06
【问题描述】:

我有一个包含 3 行的 WPF 网格,我希望按比例调整大小,除非其中一列或多列没有内容。每行当前都有一个gridview,如果没有行,我想将其他2列的大小相同。如果只有一行有数据,我希望该行占据所有空间。

等高行:

只有 2 行数据的等高

我目前的网格定义如下:

<Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

这将清楚地平均分配所有 3 列的高度。我想知道如何根据数据是否存在动态地在同一列上使用 Auto 和 * sizing。

【问题讨论】:

标签: wpf gridview


【解决方案1】:

据我了解,Grid 是一个容器,这意味着 Grid 在实际内容之前呈现。从技术上讲,不可能根据内容设置动态高度/宽度(除非您设置为自动)。通常这种情况可以通过控制控件的宽度/高度来克服,而不是解决复杂的解决方案。简而言之,假设您有 3 个控件,并且必须渲染 2 个控件中的任何一个,这必须同样占据页面宽度/高度,您可以将网格行高设置为自动,同时设置内容高度。可以参考父控件的实际宽度。

这里是示例供参考。在这个例子中,我假设你有一些机制来指导网格现在有多少用户控件是可见的。所以在converterParameter中传递项目计数。

 public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int.TryParse(parameter.ToString(), out var items);
        double.TryParse(value.ToString(), out var actualheight);
        return actualheight / items;
    }
         //......    
  }

 <Window.Resources>
    <local:HeightConverter x:Key="HeightConverter"/>
</Window.Resources>
<Grid x:Name="mainGrid" Background="Gray">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>

    </Grid.RowDefinitions>
    <local:sampleControl Background="Red" Grid.Row="0" Height="{Binding ElementName=mainGrid, Path=ActualHeight, Converter={StaticResource HeightConverter}, ConverterParameter=3}"/>
    <local:sampleControl Background="Yellow" Grid.Row="1" Height="{Binding ElementName=mainGrid, Path=ActualHeight, Converter={StaticResource HeightConverter}, ConverterParameter=3}"/>

    <local:sampleControl Background="Blue" Grid.Row="2" Height="{Binding ElementName=mainGrid, Path=ActualHeight, Converter={StaticResource HeightConverter}, ConverterParameter=3}"/>

</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2013-12-06
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多