gpt4 book ai didi

wpf - 如何解决 XAML 中的这个拉伸(stretch)问题?

转载 作者:行者123 更新时间:2023-12-04 17:26:36 27 4
gpt4 key购买 nike

我有以下 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="*"/> .例如,如果只有一个 Grid 填充,则它不适合整个父网格。如果我删除三个 RowDefinitions , 但它有效。

附加信息:非填充网格可见性始终设置为 Visibility.Collapsed .

最佳答案

我总是发现,如果你想隐藏一个列或行,你可以像这样绑定(bind)一个属性:

<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

关于wpf - 如何解决 XAML 中的这个拉伸(stretch)问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975996/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com