WPF 如何使边框宽度扩展以填充 DockPanel 中的可用空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25122719/
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-13 12:16:39  来源:igfitidea点击:

WPF How do I make a Border width expand to fill available space in a DockPanel

c#wpfwpf-controls

提问by Lex Webb

I have a custom control ButtonRowwhich will end up going into a different control.

我有一个自定义控件ButtonRow,它最终会进入不同的控件。

It is very simple, it has one Border, on label and one button.

它非常简单,它有一个边框、标签和一个按钮。

I need to make it so that the border will extend its width to fill up to where the button is. This is not happening as you can see in the below image:

我需要这样做,以便边框将扩展其宽度以填充按钮所在的位置。这不会发生,如下图所示:

Control Preview

控制预览

The XAML can be found below. I have tried fiddling about with the horizontal alignment of both he label and the border, but they will only ever re-size to fit the text content of the label.

XAML 可以在下面找到。我曾尝试摆弄标签和边框的水平对齐方式,但它们只会重新调整大小以适应标签的文本内容。

I know there are existing question with very similar problems and names, but none have needed to do quite the same thing or have helped me solve my problem.

我知道存在具有非常相似的问题和名称的问题,但没有一个需要做完全相同的事情或帮助我解决我的问题。

I have tried using a StackPanel in horizontal alignment but all it did was make the button go next to the border.

我曾尝试在水平对齐中使用 StackPanel,但它所做的只是让按钮靠近边框。

How can I make the border expand to fill the available space?

如何使边框扩展以填充可用空间?

<Grid>
    <DockPanel Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto">
        <Border BorderBrush="#FFDADFE1" Background="#FFECF0F1" BorderThickness="1" Height="20" Name="bdrFilter" HorizontalAlignment="Stretch">
            <Label Content="Filter..." FontStyle="Italic" Foreground="#FF6C7A89" Height="20" Name="lblFilter" Padding="5,0" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" />
        </Border>
        <Button Style="{StaticResource FlatButtonStyle}" Content="+" Height="20" HorizontalAlignment="Right" Name="btnAddFilter" VerticalAlignment="Top" Width="20" Foreground="#FF6C7A89" ForceCursor="True" BorderBrush="{x:Null}" />
    </DockPanel>
</Grid>

(The button style does not affect its alignment or any other relevant properties)

(按钮样式不影响其对齐方式或任何其他相关属性)

回答by Sheridan

A DockPanelis not the correct Panelto use for this requirement... like a StackPanel, it does notresize its contents. Instead, just use a regular Grid(which also uses less resources than a DockPanel):

一个DockPanel是不正确的Panel使用这一要求......像StackPanel,它并没有调整其内容。相反,只需使用常规Grid(它使用的资源也比 a 少DockPanel):

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Border BorderBrush="#FFDADFE1" Background="#FFECF0F1" BorderThickness="1" 
        Height="20" Name="bdrFilter" VerticalAlignment="Top">
        <Label Content="Filter..." FontStyle="Italic" Foreground="#FF6C7A89" 
            Height="20" Name="lblFilter" Padding="5,0" />
    </Border>
    <Button Grid.Column="1" Content="+" Height="20" HorizontalAlignment="Right" 
        Name="btnAddFilter" VerticalAlignment="Top" Width="20" Foreground="#FF6C7A89" 
        ForceCursor="True" BorderBrush="{x:Null}" />
</Grid>

Please see the Panels Overviewpage on MSDN for more information about the different Panels in WPF.

有关WPF 中不同s 的更多信息,请参阅MSDN 上的面板概述页面Panel