问题
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