WPF: Shared Size in Custom Panel?

天涯浪子 提交于 2019-12-11 05:24:07

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5734184/wpf-shared-size-in-custom-panel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!