如何在 WPF 中獲取控件以填充可用空間? - How to get controls in WPF to fill available space?

問題:

Some WPF controls (like the Button ) seem to happily consume all the available space in its' container if you don't specify the height it is to have.如果您沒有指定它的高度,一些 WPF 控件(如Button )似乎很樂意消耗其容器中的所有可用空間。

And some, like the ones I need to use right now, the (multiline) TextBox and the ListBox seem more worried about just taking the space necessary to fit their contents, and no more.還有一些,比如我現在需要使用的那些,(多行) TextBoxListBox似乎更擔心只佔用適合其內容所需的空間,僅此ListBox

If you put these guys in a cell in a UniformGrid , they will expand to fit the available space.如果您將這些傢伙放在UniformGrid一個單元格中,它們將擴展以適應可用空間。 However, UniformGrid instances are not right for all situations.然而, UniformGrid實例並不適合所有情況。 What if you have a grid with some rows set to a * height to divide the height between itself and other * rows?如果您有一個網格,其中一些行設置爲 * 高度以劃分其自身和其他 * 行之間的高度,該怎麼辦? What if you have a StackPanel and you have a Label , a List and a Button , how can you get the list to take up all the space not eaten by the label and the button?如果你有一個StackPanel並且你有一個Label ,一個List和一個Button ,你怎麼能讓列表佔據標籤和按鈕沒有佔用的所有空間?

I would think this would really be a basic layout requirement, but I can't figure out how to get them to fill the space that they could (putting them in a DockPanel and setting it to fill also doesn't work, it seems, since the DockPanel only takes up the space needed by its' subcontrols).我認爲這真的是一個基本的佈局要求,但我不知道如何讓它們填充它們可以填充的空間(將它們放在DockPanel並將其設置爲填充也不起作用,似乎,因爲DockPanel只佔用其子控件所需的空間)。

A resizable GUI would be quite horrible if you had to play with Height , Width , MinHeight , MinWidth etc.如果您必須使用HeightWidthMinHeightMinWidth等, MinHeight調整大小的 GUI 會非常糟糕。

Can you bind your Height and Width properties to the grid cell you occupy?你能將你的HeightWidth屬性綁定到你佔據的網格單元嗎? Or is there another way to do this?或者有另一種方法可以做到這一點?


解決方案:

參考一: https://stackoom.com/question/9OO
參考二: How to get controls in WPF to fill available space?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章