WPF:自定义面板中的共享大小?

发布于 2024-11-02 06:48:49 字数 1220 浏览 3 评论 0原文

我试图在网格的列/行定义中实现类似于 SharedSizeGroup 的东西。

简化情况:

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

根面板正在定义范围。所有具有相同 SharedWidth 的元素都应具有相同的宽度。我已经实现了自定义测量和排列行为,但我正在努力让它与 SharedSize 模型一起使用。我怀疑我必须以某种方式进行两次安排过程?第一个找出默认大小(并收集最大宽度),然后再进行一次以使用此最大宽度作为排列结果中的参数。但我该怎么做呢?我想我不能在每个 ArrangeOverride 中运行两次传递,因为这会导致每个元素对其整个降序元素树进行两次传递?嗯嗯。有什么建议吗?

更新

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

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>

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

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

Simplified case:

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

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?

UPDATE

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...

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

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

发布评论

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

评论(1

回梦 2024-11-09 06:48:49

幸运的是,整个树的这两个通道已经内置到布局系统中。首先在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文