WPF:如何拉伸DockPanel中的中间孩子?

我添加了一个DockPanel到RadioButton元素,这样我就可以使用100%的宽度水平分配单选button标签,文本框和button。

在DockPanel中使用LastChildFill="True"可以延伸最后一个元素。 如果文本框是面板中的最后一个孩子,这很好地工作。 但是,由于button是最后一个元素并且具有固定的宽度,所以文本框应该被拉伸。 但是,没有像2ndChildFill="True"这样的属性。

我的代码如下所示:

  <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> 

它给了我这个:

wpf截图

任何想法,提示来解决这个问题? 提前谢谢了…

您需要为您的元素设置DockPanel.Dock附加属性,并将TextBox作为最后一个元素:

 <RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <DockPanel> <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>