【问题标题】:WPF - Stretch expander when having HorizontalAlignment set to Left on pageWPF - 在页面上将 Horizo​​ntalAlignment 设置为 Left 时拉伸扩展器
【发布时间】:2018-01-19 15:23:24
【问题描述】:

所以我想知道在用户控件/页面上将 Horizo​​ntalAlignment 设置为左​​时是否可以在页面上拉伸扩展器。

问题是我们不想在扩展器上设置宽度,因为我们希望在调整应用程序大小时调整它的大小。我们也不能在扩展器上设置最小宽度,因为我们的应用程序也有 850 的最小宽度和 1200 的正常宽度,我们希望扩展器始终拉伸到最大尺寸。

代码示例:

<UserControl x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        d:DesignWidth="1100" HorizontalAlignment="Left">
  <DockPanel>
    <StackPanel>
      <Expander Background="LightBlue"/>
    </StackPanel>
  </DockPanel>
</UserControl>

我们的主要目标是创建如下图所示的东西,其中单选按钮始终位于右侧,并且扩展器的大小适合其可用空间。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    如果您使用DockPanel,只需将其Dock 属性用于子元素并设置LastChildFill="True"Expander 当然应该是最后一个子元素)。

    类似这样的:

    <DockPanel LastChildFill="True">
         <RadioButton DockPanel.Dock="Right" Content="Active"/>
         <Expander DockPanel.Dock="Left" Header="title1" Background="LightBlue"/>
    </DockPanel>
    

    【讨论】:

    • 是的,这也是一个解决方案,如果我是正确的,LastChildFill 默认为 true。我会将其标记为此问题的解决方案。
    • @ThomasV 是,默认值为True
    【解决方案2】:

    您可以将您使用控件的HorizontalAlignment 设置为Stretch 吗?

    <Window ...>
        <Grid>
            <local:Window1 HorizontalAlignment="Stretch"></local:MainWindow>
        </Grid>
    </Window>
    

    local:Window1 是问题中定义的用户控件)

    【讨论】:

    • 您好,我们将用户控件加载到框架中,因此这是不可能的。但是我们已经解决了这个问题。我会发布我的答案。无论如何感谢您的反馈。
    【解决方案3】:

    我们通过在扩展器标题中使用列宽设置为 auto 和 * 的网格解决了这个问题:

      <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="auto"/>
              <ColumnDefinition Width="auto"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-13
      • 2010-09-21
      • 2019-01-26
      • 2011-05-21
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多