c# - WPF:如何在 DockPanel 中拉伸(stretch)中间的 child ?

标签 c# wpf xaml textbox dockpanel

我将 DockPanel 添加到 RadioButton 元素,这样我就可以使用 100% 的宽度水平分布单选按钮标签、文本框和按钮。

在 DockPanel 中使用 LastChildFill="True" 拉伸(stretch)最后一个元素。如果文本框是面板中的最后一个子项,则效果很好。但是,由于按钮是最后一个元素并且具有固定宽度,因此应该拉伸(stretch)文本框。但是,没有像 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 screenshot

有任何解决此问题的想法和提示吗?非常感谢...

最佳答案

你需要设置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>

关于c# - WPF:如何在 DockPanel 中拉伸(stretch)中间的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14034674/

相关文章:

c# - 为什么这种简单的风格不适用?

c# - 在程序集中创建一个 Hello World 库函数并从 C# 中调用它

c# - ASP.NET Core 中的 SnakeCaseNamingStrategy 和 JsonPatch

c# - 从字典中排除单词

c# - 将转换器绑定(bind)为内部类?

c# - DataGrid 的混淆行为

c# - 解码 url 并存储为具有特殊字符的字符串

c# - 如何创建具有不同类型的单个元素的类型安全数组或集合?

c# - URF 与 WPF MVVM Caliburn Micro

wpf - 你推荐 wpf 用于 POS