WPF:自定义面板中的共享大小? [英] WPF: Shared Size in Custom Panel?

查看:102
本文介绍了WPF:自定义面板中的共享大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现类似于Column/RowDefinition for Grids中的SharedSizeGroup的功能.

Im trying to implement something similar to SharedSizeGroup in Column/RowDefinition for Grids.

简化的情况:

<l:MyCustomPanel>
  <l:MyCustomPanel>
    <TextBlock l:MyCustomPanel.SharedWidth="t1" />
  </l:MyCustomPanel>
  <l:MyCustomPanel>
    <TextBlock l:MyCustomPanel.SharedWidth="t1" />
  </l:MyCustomPanel>
</l:MyCustomPanel>

根面板正在定义范围.具有相同SharedWidth的所有元素应具有相同的宽度.我已经实现了自定义的Measure和Arrange-behaviours,但是Im难以使它与SharedSize-model一起使用.我怀疑我必须以某种方式进行两次整理过程吗?首先查找默认大小(并收集最大宽度),然后再进行一次传递以将此最大宽度用作安排结果中的参数.但是我该怎么办呢?我猜我不能在每个ArrangeOverride中运行两次遍历,因为这会使每个元素对整个降序树进行两次遍历?嗯有什么建议吗?

The root-panel is defining the scope. All elements with the same SharedWidth should have the same width. I have implemented my custom Measure and Arrange-behaviours but Im struggling to get it to work with the SharedSize-model. I suspect I have to make two passes of the arrange-process somehow? First one to find out the default-sizes (and gather the max width) and then one more pass to use this max width as a parameter in the arrange-result. But how would I do this? I guess I cant run two passes in each ArrangeOverride because this would case every element to make two passes of its entire tree of descending elements? Hmmmmm. Any suggestions?

更新

此代码更详细地说明了该问题:

This code illustrates the problem more in detail:

public class CustomPanel : Panel
{
    protected override Size MeasureOverride(Size availableSize)
    {
        Children[0].Measure(availableSize);
        return Children[0].DesiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        Children[0].Arrange(new Rect(new Point(), new Size(finalSize.Width / 2, finalSize.Height)));
        return finalSize;
    }
}

<StackPanel>
  <l:CustomPanel>
    <TextBox />
  </l:CustomPanel>
</StackPanel>

在文本框中输入文本使其溢出时,它会扩展到无法使用的空间...

When entering text in the textbox making it overflow, it expands into space made unavailable...

推荐答案

很幸运,整棵树的这两次遍历已经内置到布局系统中.首先在MeasureOverride传递过程中收集所有自然尺寸,然后在ArrangeOverride传递过程中使用该信息来选择共享的宽度值.

As luck would have it, those two passes of the entire tree are already built into the layout system. First collect all the natural sizes during the MeasureOverride pass, and then use that information during the ArrangeOverride pass to choose the shared width values.

这篇关于WPF:自定义面板中的共享大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆