如何在XAML中解决这个拉伸问题?

时间:2012-10-19 14:06:06

标签: wpf silverlight xaml

我有以下XAML(简化):

<Grid x:Name="parentGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <!-- content which fits its parent grid -->
    </Grid>

    <Grid Grid.Row="1">
        <!-- content which fits its parent grid -->
    </Grid>

    <Grid Grid.Row="2">
        <!-- content which fits its parent grid -->
    </Grid>

    <Grid Grid.Row="3">
        <!-- content which fits its parent grid -->
    </Grid>
</Grid>

这是一个可配置的容器,它包含一到四个对话框。这个容器被重复使用,包含对话框的数量总是不同。

要求是,如果只有一个对话框(所以只填充第一个网格),它应该延伸到整个父网格。如果填充了两个网格,则每个容器应填充父网格的一半。如果有三个......等等。

我无法使用<RowDefinition Height="Auto"/>(默认为默认)和<RowDefinition Height="*"/>。例如,如果只填充了一个网格,则它不适合整个父网格。如果我删除了三个RowDefinitions,它可以正常工作。

其他信息:未填充的网格可见性始终设置为Visibility.Collapsed

2 个答案:

答案 0 :(得分:0)

我总是发现,如果你想隐藏你将属性绑定到它的列或行,就像这样:

<Grid x:Name="ParentGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding GridRow0}" />
        <RowDefinition Height="{Binding GridRow1}" />
        <RowDefinition Height="{Binding GridRow2}" />
        <RowDefinition Height="{Binding GridRow3}" />
    </Grid.RowDefinitions>
<-- grid contents -->
</Grid>

设置如下属性:

Public Property GridRow0 as GridLength = New GridLength(GridUnitType.Star)
Public Property GridRow1 as GridLength = New GridLength(GridUnitType.Star)
Public Property GridRow2 as GridLength = New GridLength(GridUnitType.Star)
Public Property GridRow3 as GridLength = New GridLength(GridUnitType.Star)

然后当你弄清楚需要多少行时,你会做一些事情,比如把计数传递给一个程序:

Public Sub FixRows(count as Integer)
    Select Case count
        Case 1
            GridRow3 = New GridLength(0)
            GridRow2 = New GridLength(0)
            GridRow1 = New GridLength(0)
            GridRow0 = New GridLength(GridUnitType.Star)
        Case 2
            GridRow3 = New GridLength(0)
            GridRow2 = New GridLength(0)
            GridRow1 = New GridLength(GridUnitType.Star)
            GridRow0 = New GridLength(GridUnitType.Star)
        Case 3
            GridRow3 = New GridLength(0)
            GridRow2 = New GridLength(GridUnitType.Star)
            GridRow1 = New GridLength(GridUnitType.Star)
            GridRow0 = New GridLength(GridUnitType.Star)
        Case 4
            GridRow3 = New GridLength(GridUnitType.Star)
            GridRow2 = New GridLength(GridUnitType.Star)
            GridRow1 = New GridLength(GridUnitType.Star)
            GridRow0 = New GridLength(GridUnitType.Star)
        Case Else
            'Whatever is needed
    End Select
End Sub

答案 1 :(得分:0)

我最终创建了CustomPanel和几个NestedCustomPanel。后者提供了不同的行为,例如一个只是用于容纳一个可配置的容器,另一个用于在屏幕上均匀地排列多个容器。

在运行时,读取了配置,并创建了要添加的特定部分,并将其添加到后面的代码中。

这种方式为我可以获得的每个可能的配置提供了自定义行为。为了反映我在我的首发帖子中提供的代码:添加的RowDefinitions与配置中的容器一样多。{/ p>