简体   繁体   English

如果对等元素的大小不是独立的,如何实现WPF布局?

[英]How do I implement WPF layout if sizes of peer elements are not independent?

Imagine I have two WPF FrameworkElements that need to be laid out. 想象一下,我有两个WPF FrameworkElements需要布置。 One element contains text, and the other shows line numbers for the text. 一个元素包含文本,另一个元素显示文本的行号。

The widths of these two elements are not independent. 这两个元素的宽度不是独立的。 For example, if the text element gets narrower, then more lines may wrap, which increases the number of lines, which may cause the line number element to grow wider (eg if the number of lines goes from 99 to 100). 例如,如果文本元素变窄,则可能会缠绕更多行,这会增加行数,这可能导致行数元素变宽(例如,如果行数从99变为100)。 But if the line number element grows wider, then there's less space for the text element, etc. 但是,如果行号元素变宽,则文本元素等的空间将减少。

How do I implement layout in WPF if the sizes of my child elements need to be calculated in concert with one another? 如果我的子元素的大小需要相互计算,该如何在WPF中实现布局? I am new to WPF layout, but it looks like a fundamental assumption of MeasureOverride is that an element's size can be calculated without regard to the sizes of peers. 我是WPF布局的新手,但它似乎是MeasureOverride的基本假设,即可以计算元素的大小而无需考虑对等项的大小。

Thanks for any help. 谢谢你的帮助。

Sounds to me you need to implement the layout in the container. 在我看来,您需要在容器中实现布局。 See http://www.wpftutorial.net/CustomLayoutPanel.html on how to implement your own layout panel. 有关如何实现自己的布局面板的信息,请参见http://www.wpftutorial.net/CustomLayoutPanel.html I think a Grid layout can do what you want. 我认为网格布局可以满足您的需求。 See http://www.wpftutorial.net/GridLayout.html 参见http://www.wpftutorial.net/GridLayout.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM