【问题标题】:WPF How do I make a Border width expand to fill available space in a DockPanelWPF 如何使边框宽度扩大以填充 DockPanel 中的可用空间
【发布时间】:2014-08-04 15:53:44
【问题描述】:

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

很简单,一个边框,一个标签,一个按钮。

我需要这样做,以便边框扩展其宽度以填充到按钮所在的位置。 正如您在下图中看到的那样,这并没有发生:

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

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

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

如何使边框扩大以填满可用空间?

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

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

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    DockPanel 不是用于此要求的正确 Panel... 就像 StackPanel,它确实调整其内容的大小。相反,只需使用常规的Grid(它也比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>
    

    有关 WPF 中不同 Panels 的更多信息,请参阅 MSDN 上的 Panels Overview 页面。

    【讨论】:

      猜你喜欢
      • 2010-11-08
      • 2016-12-02
      • 2020-06-30
      • 2020-03-09
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      相关资源
      最近更新 更多