wpf 水平 Stackpanel 填充父控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9912072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:20:25  来源:igfitidea点击:

Horizontal Stackpanel Fill Parent Control

wpfxamllayoutstackpanel

提问by Ternary

Is it possible to have a <StackPanel>or labels (or buttons, whatever) going horizontally across it's parent control (e.g. Window) and have it fill out all the space allowed?

是否可以让<StackPanel>或 标签(或按钮等)水平穿过它的父控件(例如Window)并填充所有允许的空间?

For example if I had 3 controls

例如,如果我有 3 个控件

_window width__

[ 1 ]__[ 2 ] __[ 3 ]

_窗口宽度_ _

[1] __[2] __[3]

or 2 controls

或 2 个控件

_window width__

[ 1 ]_______[ 2 ]

_窗口宽度_ _

[ 1 ] ___ ___ _[ 2 ]

in each case the full width of the window is being utilized with each edge control being left and right justified respectively.

在每种情况下,窗口的整个宽度都被利用,每个边缘控件分别左对齐和右对齐。

回答by Matt Burland

A StackPanel will stack controls, so no is the short answer. That's not what a StackPanel is designed to do.

StackPanel 将堆叠控件,所以不是简单的答案。这不是 StackPanel 的设计目的。

You could use a grid as Vlad suggested. Something like this:

您可以按照 Vlad 的建议使用网格。像这样的东西:

    <Grid HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Button Width="20" Height="20" Grid.Column="0"/>
        <Button Width="20" Height="20" Grid.Column="2"/>
        <Button Width="20" Height="20" Grid.Column="4"/>
    </Grid>

回答by user2940256

        StackPanel m_pstackpanel = (StackPanel)this.Parent;
        m_pstackpanel.Children.Clear();
        keypadclasslist keypadWindow = new keypadclasslist(m_pLstReceiver);
        m_pstackpanel.Children.Add(keypadWindow);