简体   繁体   English

如何在 WPF 中使更改的列数填充整个宽度并水平居中?

[英]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.

相关问题 如何使 WPF 数据模板填充列表框的整个宽度? - How do I make a WPF data template fill the entire width of the listbox? 使WPF ComboBoxes填充整个列宽 - Make WPF ComboBoxes fill a whole column width WPF StatusBar具有单个StatusBarItem和ComboBox来填充整个宽度 - WPF StatusBar with single StatusBarItem and ComboBox to fill the entire width 最大化窗口时WPF Datagrid不会填充整个高度和宽度 - WPF Datagrid does not fill the entire height and width when maximizing window 与WPF中的其他内联相比,如何将内联(Run)水平居中 - How to horizontally center an inline (Run) comparing to other inlines in WPF 如何获取按钮内容以填充WPF中的宽度 - How to get button content to fill width in WPF WPF 如何使边框宽度扩大以填充 DockPanel 中的可用空间 - WPF How do I make a Border width expand to fill available space in a DockPanel 当 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 不起作用时,如何使 WPF 中的控件拉伸以填充可用宽度? - How can I make controls in WPF stretch to fill available width when HorizontalAlignment and HorizontalContentAlignment don't work? 如何使我的整个选项卡在WPF中 - How to make my entire tab clicakble in WPF 如何确保WPF网格中的列遵守最小宽度? - How do I make sure columns in a WPF Grid respect a minimum width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM