[英]How to make changing number of columns fill entire width and center horizontally in WPF?
I have a Grid with 3 Columns in WPF.我在 WPF 中有一个包含 3 列的网格。 Each column is filled with a Vertical Stackpanel.
每列都填充了一个垂直堆栈面板。 The first column (and its content) is always visible.
第一列(及其内容)始终可见。 the 2. and 3. column are linked to checkboxes.
2. 和 3. 列链接到复选框。
And i basically want to have the columns fill the entire horizontal space while also having their content centered (the content of each column is the same) .而且我基本上想让列填充整个水平空间,同时还让它们的内容居中(每列的内容相同)。 So for example if only the first column is used, its content should be centered over the whole grid width.
因此,例如,如果仅使用第一列,则其内容应在整个网格宽度上居中。 If the 2. column is used aswell, The whole grid space should be equaly divided for both columns and their content should be centered inside.
如果同时使用 2. 列,则两列的整个网格空间应均等划分,并且它们的内容应在内部居中。 With also the third column used, the space would be divided by 3 of course.
还使用了第三列,当然空间将被 3 除。
My idea for now was the following but i cant get the stackpanels to be centered/fill out the horizontal space.我现在的想法如下,但我无法让堆栈面板居中/填充水平空间。
<CheckBox Style="{StaticResource MaterialDesignCheckBox}" Content="2. Durchgang" IsChecked="{Binding RVPDGsecondround}" Margin ="0,0,0,0"/>
<CheckBox Style="{StaticResource MaterialDesignCheckBox}" Content="3. Durchgang" IsChecked="{Binding RVPDGthirdround}" Margin ="0,0,0,0"/>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Grid.Column="0">
<TextBlock Text="Durchgang 1" Style="{StaticResource MaterialDesignBody2TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Grid.Column="1" Visibility="{Binding RVPDGsecondround, Converter={StaticResource b2v}}">
<TextBlock Text="Durchgang 2" Style="{StaticResource MaterialDesignBody2TextBlock}"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Grid.Column="2" Visibility="{Binding RVPDGthirdround, Converter={StaticResource b2v}}">
<TextBlock Text="Durchgang 3" Style="{StaticResource MaterialDesignBody2TextBlock}" />
</StackPanel>
</Grid>
A UniformGrid may be better suited than a Grid: UniformGrid 可能比 Grid 更适合:
<UniformGrid Rows="1" HorizontalAlignment="Stretch">
<StackPanel HorizontalAlignment="Center">
<TextBlock Text="Column 1"/>
</StackPanel>
<StackPanel HorizontalAlignment="Center" Visibility=...>
<TextBlock Text="Column 2"/>
</StackPanel>
<StackPanel HorizontalAlignment="Center" Visibility=...>
<TextBlock Text="Column 3"/>
</StackPanel>
</UniformGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.