使用什么 WPF 面板控件将子项锚定到边缘(手风琴效果)?

发布于 2024-11-03 16:53:49 字数 362 浏览 4 评论 0原文

这(希望)是一个简单的...我正在尝试找到一个可以使用的 WPF 面板,当调整大小时,它会保持所有垂直对齐的子项(按钮)与面板底部边缘的偏移距离相同,因此它看起来他们正在扩张。

最明显的方法是使用停靠面板并锚定到底部,但这似乎不起作用。将两个以上的孩子放在那里会弄乱对齐方式,无论我做什么,我都无法让它们垂直对齐。我尝试过各种面板,但没有任何乐趣。我认为这相当简单,但它难倒了我!

基本上,当我单击顶部按钮时,我试图获得手风琴效果,面板会展开并显示所有子按钮。当我再次单击顶部按钮时,它会折叠。我想我可以用故事板移动每个孩子,但我必须认为我的故事需要做的就是改变面板的大小,并且孩子们保持他们的偏移和手风琴......

有什么想法吗?

提前致谢!

This (hopefully) is an easy one... I'm trying to find a WPF panel that I can use that when resized keeps all it's vertically aligned children (buttons) offset the same distance from the bottom edge of the panel, so it looks like they are expanding.

The obvious one is to use a dock panel and anchor to the bottom but this doesn't seem to work. Putting more that 2 children in there messes up the alignment and whatever I do I can't get them vertically lined up. I've tried all sorts of panels but have had no joy. I'm assuming it's fairly straightforward but it's stumped me!

Basically I'm trying to get a concertina effect when I click the top button the panel expands and shows all the sub buttons. And when I click the top button again it collapses. I guess I can move each of the children with a storyboard but I have to think all my story needs to do is change the size of the panel and the children maintain their offsets and concertina out...

Any ideas?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-11-10 16:53:49

如果“基本上我想获得手风琴效果”是您问题的本质(我对吗?)那么,您是否尝试过使用内置的 WPF 扩展器?在我看来,你正试图建立自己的......

If "Basically I'm trying to get a concertina effect" is the essence of your question (Am I right ?) then, did you tried tu use an built-in WPF expander ? It seems to me that you are trying to build up your own...

可遇━不可求 2024-11-10 16:53:49

我相信你实际上可以使用Canvas并设置控件的Canvas.Top、Left、Right和Bottom属性来获得与WinForms和anchor相同的效果。

<Canvas>
    <Button Canvas.Left="30" Canvas.Bottom="10" Content="Button 1"  Name="button1"  />
    <Button Canvas.Left="90" Canvas.Bottom="10" Content="Button 2"  Name="button2"  />
</Canvas>

在此处输入图像描述

Grid 也可以执行类似操作:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>
    <Button Grid.Row="1" Grid.Column="0" Content="Button 1"  Name="button1" Margin="5" />
    <Button Grid.Row="1" Grid.Column="1" Content="Button 2"  Name="button2" Margin="5" />
</Grid>

I believe you can actually use a Canvas and set the Canvas.Top, Left, Right, and Bottom properties of the controls to get the same effect as WinForms and anchor.

<Canvas>
    <Button Canvas.Left="30" Canvas.Bottom="10" Content="Button 1"  Name="button1"  />
    <Button Canvas.Left="90" Canvas.Bottom="10" Content="Button 2"  Name="button2"  />
</Canvas>

enter image description here

Also Grid can do similar:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>
    <Button Grid.Row="1" Grid.Column="0" Content="Button 1"  Name="button1" Margin="5" />
    <Button Grid.Row="1" Grid.Column="1" Content="Button 2"  Name="button2" Margin="5" />
</Grid>
揽月 2024-11-10 16:53:49

我不太明白你所说的手风琴效应是什么意思,它也可能与拉伸有关。如果你想要锚定,你可以使用网格:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition /> <!-- Gets all available space i.e. resizes with window -->
        <RowDefinition Height="Auto"/> <!-- Sizes to content, always stays at the bottom -->
    </Grid.RowDefinitions>
    <!-- Resizable content goes in Grid.Row="0" -->
    <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="5">
        <Button Name="ButtonOK" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonOK_Click" IsDefault="True">OK</Button>
        <Button Name="ButtonCancel" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonCancel_Click" IsCancel="True">Cancel</Button>
    </StackPanel>
</Grid>

I do not quite get what you mean by concertina effect, it could have to do with stretching as well. If you want anchoring you can use a Grid:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition /> <!-- Gets all available space i.e. resizes with window -->
        <RowDefinition Height="Auto"/> <!-- Sizes to content, always stays at the bottom -->
    </Grid.RowDefinitions>
    <!-- Resizable content goes in Grid.Row="0" -->
    <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="5">
        <Button Name="ButtonOK" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonOK_Click" IsDefault="True">OK</Button>
        <Button Name="ButtonCancel" HorizontalAlignment="Right" Width="100" Margin="5" Click="ButtonCancel_Click" IsCancel="True">Cancel</Button>
    </StackPanel>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文