WPF:如何在 DockPanel 中拉伸中间的孩子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14034674/
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
WPF: How can I stretch the middle child in a DockPanel?
提问by brgn
I added a DockPanel to a RadioButton element such that I can distribute the radio button label, a textbox and a button horizontally using 100% of the width.
我向 RadioButton 元素添加了一个 DockPanel,这样我就可以使用 100% 的宽度水平分布单选按钮标签、文本框和按钮。
Using LastChildFill="True"within the DockPanel stretches the last element. This works out nicely if the textbox is the last child in the panel. But, as the button is the last element and has a fixed width, the textbox should be stretched. However, there's no such property like 2ndChildFill="True".
LastChildFill="True"在 DockPanel 中使用会拉伸最后一个元素。如果文本框是面板中的最后一个子项,则效果很好。但是,由于按钮是最后一个元素并且具有固定宽度,因此应该拉伸文本框。但是,没有像2ndChildFill="True".
My code looks like this:
我的代码如下所示:
<RadioButton HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<DockPanel >
<TextBlock VerticalAlignment="Center">in location:</TextBlock>
<TextBox Grid.Column="1" Margin="10,0,0,0">Path string</TextBox>
<Button HorizontalAlignment="Right"
Margin="10,0,0,0" Padding="3,0">...</Button>
</DockPanel>
</RadioButton>
And it gives me this:
它给了我这个:


Any ideas, hints to fix this? Many thanks in advance...
任何想法,解决这个问题的提示?提前谢谢了...
回答by max
You need to set DockPanel.Dockattached property for your elements and leave TextBoxas the last element:
您需要DockPanel.Dock为元素设置附加属性并保留TextBox为最后一个元素:
<RadioButton HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Left"
VerticalAlignment="Center"
Text="in location:" />
<Button DockPanel.Dock="Right"
Margin="10,0,0,0"
Padding="3,0"
Content="..." />
<TextBox Margin="10,0,0,0">
Path string
</TextBox>
</DockPanel>
</RadioButton>

