动态调整打开的手风琴的大小

发布于 2024-09-01 12:16:38 字数 1606 浏览 9 评论 0原文

我有一个手风琴,其内容的高度可以动态调整大小。我希望看到手风琴动态响应子项的高度,但我在执行此操作时遇到困难。

    <lt:Accordion Name="MyAccordion"
                  SelectionMode="ZeroOrOne"
                  HorizontalAlignment="Stretch">
        <lt:AccordionItem Name="MyAccordionItem"
                          Header="MyAccordion"
                          IsSelected="True"
                          HorizontalContentAlignment="Stretch"
                          VerticalAlignment="Stretch">
            <StackPanel>
                <Button Content="Grow" Click="Grow"/>
                <Button Content="Shrink" Click="Shrink"/>
                <TextBox Name="GrowTextBox"
                         Text="GrowTextBox"
                         Height="400"
                         Background="Green"
                         SizeChanged="GrowTextBox_SizeChanged"/>
            </StackPanel>
        </lt:AccordionItem>
    </lt:Accordion>


    private void Grow(object sender, System.Windows.RoutedEventArgs e)
    {
        GrowTextBox.Height += 100;
    }

    private void Shrink(object sender, System.Windows.RoutedEventArgs e)
    {
        GrowTextBox.Height -= 100;
    }

    private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
        MyAccordion.UpdateLayout();
        MyAccordionItem.UpdateLayout();
    }

请注意,如果我折叠然后重新打开手风琴,它就会按照我想要的方式成形,但我希望在孩子调整大小时立即进行大小调整。

我无力地尝试通过添加在 Accordion 和 AccordionItem 上调用 UpdateLayout() 的 SizeChanged 事件处理程序来解决此问题,但这没有任何视觉效果。我无法弄清楚 Accordion 控件中的适当调整大小的位置。有人有想法吗?

I have an Accordion and the height of its content can be dynamically resized. I would like to see the Accordion dynamically respond to the child item's height, but I'm having trouble doing this.

    <lt:Accordion Name="MyAccordion"
                  SelectionMode="ZeroOrOne"
                  HorizontalAlignment="Stretch">
        <lt:AccordionItem Name="MyAccordionItem"
                          Header="MyAccordion"
                          IsSelected="True"
                          HorizontalContentAlignment="Stretch"
                          VerticalAlignment="Stretch">
            <StackPanel>
                <Button Content="Grow" Click="Grow"/>
                <Button Content="Shrink" Click="Shrink"/>
                <TextBox Name="GrowTextBox"
                         Text="GrowTextBox"
                         Height="400"
                         Background="Green"
                         SizeChanged="GrowTextBox_SizeChanged"/>
            </StackPanel>
        </lt:AccordionItem>
    </lt:Accordion>


    private void Grow(object sender, System.Windows.RoutedEventArgs e)
    {
        GrowTextBox.Height += 100;
    }

    private void Shrink(object sender, System.Windows.RoutedEventArgs e)
    {
        GrowTextBox.Height -= 100;
    }

    private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
        MyAccordion.UpdateLayout();
        MyAccordionItem.UpdateLayout();
    }

Mind you, if I collapse and then re-open the accordion, it takes shape just the way I want, but I'd like this resizing to occur immediately when the child resizes.

I feebly attempted to fix this by adding a SizeChanged event handler that calls UpdateLayout() on the Accordion and AccordionItem, but this doesn't have any visual effect. I can't figure out where proper resizing takes place inside the Accordion control. Does anyone have an idea?

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

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

发布评论

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

评论(3

你不是我要的菜∠ 2024-09-08 12:16:38

试试这个

 //here i am creating a size object depending on child items height and width
        // and 25 for accordian item header...
        // if it works you can easily update the following code to avoid exceptional behaviour
        Size size = new Size();
        size.Width = GrowTextBox.ActualWidth;
        size.Height = grow.ActualHeight + shrink.ActualHeight + GrowTextBox.ActualHeight + 25;
        MyAccordion.Arrange(new Rect(size));

在上面的代码中,我只是根据子项目的大小重新排列手风琴。

Try this one

 //here i am creating a size object depending on child items height and width
        // and 25 for accordian item header...
        // if it works you can easily update the following code to avoid exceptional behaviour
        Size size = new Size();
        size.Width = GrowTextBox.ActualWidth;
        size.Height = grow.ActualHeight + shrink.ActualHeight + GrowTextBox.ActualHeight + 25;
        MyAccordion.Arrange(new Rect(size));

In the above code i am just rearranging accordion depending on child item size.

一指流沙 2024-09-08 12:16:38

我有一个类似的问题,我的简单技巧如下:

private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
        MyAccordionItem.Measure(new Size());
        MyAccordionItem.UpdateLayout();
}

希望它也适合你..

干杯

I have a similar problem, my simple hack is as follows:

private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
{
        MyAccordionItem.Measure(new Size());
        MyAccordionItem.UpdateLayout();
}

Hope it works for you too..

Cheers

所有深爱都是秘密 2024-09-08 12:16:38

我遇到了一个稍微不同的问题 - 调整窗口大小有时无法正确调整手风琴项目的大小,因此下一个项目的标题将卡在窗口下方或中间。

我通过创建一个在 SizeChanged 中启动的计时器解决了这个问题,该计时器取消选择并立即重新选择当前项目,之后布局似乎重新调整并显示正确。也许对你也有帮助。你可以省掉计时器,我引入它是为了防止用户拖动调整窗口大小时连续调用,它还因为延迟而提供了一种羽毛效果。

public partial class MyAccordion : System.Windows.Controls.Accordion
{
    private Timer _layoutUpdateTimer = new Timer(100);

    public MyAccordion
    {
        this.SizeChanged += (s, e) =>
        {
            _layoutUpdateTimer.Stop(); // prevents continuous calls
            _layoutUpdateTimer.Start();
        };
        _layoutUpdateTimer.Elapsed += (s, e) => ReselectItem();
    }

    private void ReselectItem()
    {
        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            // backup values
            int selectedIndex = this.SelectedIndex;
            AccordionSelectionMode mode = this.SelectionMode;

            // deselect
            this.SelectionMode = AccordionSelectionMode.ZeroOrOne; // allow null selection
            this.SelectedItem = null;

            // restore values (reselect)
            this.SelectionMode = mode;
            this.SelectedIndex = selectedIndex;
        }));
        _layoutUpdateTimer.Stop();
    }
}

I had a slightly different problem - resizing my window sometimes didn't correctly adjust the Accordion item size, so the header of the next item would be stuck below the window or in the middle of it.

I solved this by creating a timer that is started in SizeChanged, and that deselects and immediately reselects the current item, after which the layout seems to be readjusted and turns up correct. Might help you as well. You could dispense with the timer, I introduced it to prevent continuous calls when the user drag resizes the window, it also gives a kind of feathery effect because of the delay.

public partial class MyAccordion : System.Windows.Controls.Accordion
{
    private Timer _layoutUpdateTimer = new Timer(100);

    public MyAccordion
    {
        this.SizeChanged += (s, e) =>
        {
            _layoutUpdateTimer.Stop(); // prevents continuous calls
            _layoutUpdateTimer.Start();
        };
        _layoutUpdateTimer.Elapsed += (s, e) => ReselectItem();
    }

    private void ReselectItem()
    {
        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            // backup values
            int selectedIndex = this.SelectedIndex;
            AccordionSelectionMode mode = this.SelectionMode;

            // deselect
            this.SelectionMode = AccordionSelectionMode.ZeroOrOne; // allow null selection
            this.SelectedItem = null;

            // restore values (reselect)
            this.SelectionMode = mode;
            this.SelectedIndex = selectedIndex;
        }));
        _layoutUpdateTimer.Stop();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文