我最近试图用WPF布局问题帮助我的朋友,我似乎无法弄清楚如何让它工作,这看起来很简单,所以我想我会挖掘丰富的知识这里:)他想要的是groupbox1自动调整为maxwidth的值,然后保持锚定在左边,而groupbox右边的空间增长。所以为了保持这个简单,我现在要发布一些情况的示例代码:)如果有人有一些关于这种情况,请回复。谢谢大家!
<Window x:Class="GroupBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="147" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="151*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148" />
<ColumnDefinition Width="355*" />
</Grid.ColumnDefinitions>
<GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1">
<Grid />
</GroupBox>
<GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2">
<Grid />
</GroupBox>
</Grid>
答案 0 :(得分:2)
在ColumnDefinition上设置MaxWidth="450"
将适用于GroupBox
。
<ColumnDefinition Width="355*" MaxWidth="450"/>
如果第1列中的其他元素应该拉伸超过450,那么您可以为HorizontalAlignment="Left"
设置GroupBox
并将Width
绑定到同一列中的另一个元素。
ActualWidth
, ColumnDefinition
不是依赖属性,否则我们可以将其用作绑定的来源
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="151*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148" />
<ColumnDefinition Width="355*" />
</Grid.ColumnDefinitions>
<Rectangle Name="sizeElement" Fill="Transparent" Margin="14,12,41,8" Grid.Column="1"/>
<GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1"
HorizontalAlignment="Left"
Width="{Binding ElementName=sizeElement, Path=ActualWidth}">
<Grid />
</GroupBox>
<GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2">
<Grid />
</GroupBox>
</Grid>
答案 1 :(得分:1)
将MaxWidth =“450”移动到ColumnDefinition并将其从GroupBox中删除。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148" />
<ColumnDefinition Width="*" MaxWidth="450" />
</Grid.ColumnDefinitions>
<GroupBox
Name="groupBox1"
Header="groupBox1"
Margin="14,12,41,8"
Grid.Column="1">
</GroupBox>
<GroupBox Header="groupBox2" Margin="12,12,13,8" Name="groupBox2">
</GroupBox>
</Grid>
答案 2 :(得分:0)
您可以使用以下样式创建变通方法:
<GroupBox Header="groupBox1" Margin="14,12,41,8" Name="groupBox1" MaxWidth="450" Grid.Column="1">
<GroupBox.Style>
<Style TargetType="GroupBox">
<Style.Triggers>
<Trigger Property="ActualWidth" Value="450">
<Setter Property="Width" Value="450"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Trigger>
</Style.Triggers>
</Style>
</GroupBox.Style>
<Grid />
</GroupBox>