【问题标题】:Combine expander and grid (resizable expander)组合扩展器和网格(可调整大小的扩展器)
【发布时间】:2011-07-30 00:44:21
【问题描述】:

我想要一个可调整大小的扩展器之类的东西。我的基本想法是这样的:

<Grid HorizontalAlignment="Left">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="2" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Expander Grid.Column="0" ExpandDirection="Right">
          ...
    </Expander>

    <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />

    ...
</Grid>

这个问题:如果我移动网格拆分器并折叠扩展器,我会得到一个很大的空白区域。怎样才能让整列崩溃?还是有另一种方法可以使扩展器“可调整大小”

【问题讨论】:

标签: wpf xaml grid expander gridsplitter


【解决方案1】:

也许这将有助于解决您的“列崩溃”问题

XAML:

添加&lt;Grid&gt;Name="expGrid"并添加&lt;Expander&gt;Collapsed="Expander_Collapsed"

C#代码:

private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
  var colomnIndex = Grid.GetColumn(sender as Expander);
  var colomn = expGrid.ColumnDefinitions[colomnIndex];
  colomn.Width = GridLength.Auto;
}

【讨论】:

    【解决方案2】:

    不确定您要完成什么,但我认为从概念上讲 Grid 应该是 Expander.Content 的一部分,这对您有用吗?

    <Expander Header="Test" ExpandDirection="Right" HorizontalAlignment="Left" Background="LightBlue">
        <Expander.Content>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="5"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="Lorem ipsum dolor sit"/>
                <GridSplitter Grid.Column="1" Width="5" ResizeBehavior="PreviousAndCurrent" ResizeDirection="Columns"/>
            </Grid>
        </Expander.Content>
    </Expander>
    

    编辑:删除了第一列中的所有触发,因为它似乎没有必要。

    另外:要垂直工作,必须将 GridSplitter 的 HorizontalAlignment 设置为 Stretch,否则默认情况下它的宽度为零(当然,其他所有特定于方向的东西也必须进行调整,但这很简单)

    Horizo​​ntalAlignment 是 Microsoft .NET 属性访问器,它实际上是一个依赖属性。这个特定的依赖属性经常在子类元素中以不同的方式设置其明显的“默认”值,尤其是控件。 [...] 例如,即使 Label 直接从 FrameworkElement 继承 Horizo​​ntalAlignment,Label 控件的 Horizo​​ntalAlignment 的明显“默认值”将是 Left。这是因为该值在默认样式的 Label 中被重置,在样式的控件模板中。

    【讨论】:

    • 太好了,这正是我需要的!
    • 在我的一生中,我无法让这样的东西横向工作!
    • @DaveO:在答案中查看我的注释。
    • 将示例更新为HorizontalAlignment
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多