如何设置网格列的宽度,使其只达到需要的大小。自动在我的情况下不起作用
How to set a width of a grid column so it gets only as big as it needs to be. Auto does not work in my case
我有一个仅包含组合框的用户控件,它看起来像这样
<UserControl x:Class="LayoutProblem.ComboboxUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ComboBox Width="300" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</UserControl>
然后在另一个用户控件中我添加了一个标签,所以它看起来像这样
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Short Label" HorizontalAlignment="Right" />
<layoutProblem:ComboboxUserControl Grid.Column="1" />
<Label Content="Extra long Label" HorizontalAlignment="Right" Grid.Row="1"/>
<layoutProblem:ComboboxUserControl Grid.Column="1" Grid.Row="1"/>
</Grid>
然后下一个用户控件看起来像这样
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label HorizontalAlignment="Right" Content="Some other label" />
<TextBox Grid.Column="1"
Width="50"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
最后在 MainWindow 中,我将上面的内容放在一起,看起来像这样
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<layoutProblem:InputUserControl/>
<layoutProblem:ComboboxWithLabel Grid.Row="1" Grid.ColumnSpan="2" DataContext="{Binding DataContext2}" />
</Grid>
所以所有这些都将控件放在一起,但我不知道如何更改布局。因为现在标签应该在第 1 列,文本框和组合框应该放在第 2 列。无论我将列定义设置为 Auto、* 或什么都没有,列不会达到我想要的大小。如果我取出列定义,它突然起作用了,但我需要它们,因为还有其他东西要放在第二列中。
我在网上查了,没找到办法。
感谢您的帮助。
使用 IsSharedSizeScope = True 和 SharedSizeGroup = "" 解决。
我有一个仅包含组合框的用户控件,它看起来像这样
<UserControl x:Class="LayoutProblem.ComboboxUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ComboBox Width="300" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</UserControl>
然后在另一个用户控件中我添加了一个标签,所以它看起来像这样
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Short Label" HorizontalAlignment="Right" />
<layoutProblem:ComboboxUserControl Grid.Column="1" />
<Label Content="Extra long Label" HorizontalAlignment="Right" Grid.Row="1"/>
<layoutProblem:ComboboxUserControl Grid.Column="1" Grid.Row="1"/>
</Grid>
然后下一个用户控件看起来像这样
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label HorizontalAlignment="Right" Content="Some other label" />
<TextBox Grid.Column="1"
Width="50"
Height="30"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
最后在 MainWindow 中,我将上面的内容放在一起,看起来像这样
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<layoutProblem:InputUserControl/>
<layoutProblem:ComboboxWithLabel Grid.Row="1" Grid.ColumnSpan="2" DataContext="{Binding DataContext2}" />
</Grid>
所以所有这些都将控件放在一起,但我不知道如何更改布局。因为现在标签应该在第 1 列,文本框和组合框应该放在第 2 列。无论我将列定义设置为 Auto、* 或什么都没有,列不会达到我想要的大小。如果我取出列定义,它突然起作用了,但我需要它们,因为还有其他东西要放在第二列中。
我在网上查了,没找到办法。
感谢您的帮助。
使用 IsSharedSizeScope = True 和 SharedSizeGroup = "" 解决。