简体   繁体   English

在WPF中的窗口中均匀分布按钮

[英]Spacing buttons equally across a window in WPF

What's the best control to arrange buttons horizontally across a window in WPF? 在WPF中横向排列按钮的最佳控件是什么? I am having trouble arranging the buttons evenly while resizing the window. 在调整窗口大小时,我无法均匀地排列按钮。 I tried to use the stack panel but the buttons just stay at their position and do not space out to fill up the window space. 我试图使用堆栈面板,但按钮只是保持在他们的位置,并没有空间填充窗口空间。 Tried to use the dock panel as well but no luck yet. 试图使用停靠面板,但没有运气。

Can anybody suggest me a good way to do it or tell me if I am doing anything wrong? 任何人都可以建议我这样做或告诉我,如果我做错了什么?

Grid怎么样,甚至更简单的UniformGrid呢?

What are you doing wrong? 你做错了什么? You are ignoring MSDN section on the layout system in WPF :). 您正在忽略WPF中布局系统上的MSDN部分:)。

You can use Grid and set HorizontalAlignment/VerticalAlignment of buttons to Center, and grid column width should be *. 您可以使用Grid并将Horizo​​ntalAlignment / VerticalAlignment按钮设置为Center,网格列宽度应为*。

I'd like to see an example of how to easily get the UniformGrid to space the buttons equally if they are intended to retain their "natural" widths rather than being forced to have uniform widths. 我想看一个如何轻松获得UniformGrid按钮的示例,如果它们旨在保持其“自然”宽度而不是被迫具有均匀的宽度。

In the meantime, perhaps this isn't the best solution, but it seems to work for me and might help the next person: 与此同时,也许这不是最好的解决方案,但它似乎对我有用,可能对下一个人有所帮助:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="Auto" />
  <ColumnDefinition Width="*" />
  <ColumnDefinition Width="Auto" />
  <ColumnDefinition Width="*" />
  <ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Button x:Name="FirstBtn" Grid.Column="0" Content="_First" />
<!-- Skip column 1 so it will provide flexible space -->
<Button x:Name="SecondBtn" Grid.Column="2" Content="_Second" />
<!-- Skip column 3 -->
<Button Name="ThirdBtn" Grid.Column="4" Content="_Third" />

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

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