添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have 3 WPF buttons in a grid with 3 columns. I want each button to stretch to fill the contents of each cell, but only to a maximum width. Once that width has been achieved, I would like to then align each button according to it's grid column (left column button gets aligned to the left, middle column button gets centered, right column button gets aligned to the right). Here's the XAML I've go so far that isn't working:

< Grid HorizontalAlignment =" Stretch" > < Grid.ColumnDefinitions > < ColumnDefinition / > < ColumnDefinition / > < ColumnDefinition / > < /Grid.ColumnDefinitions > < Button HorizontalAlignment =" Left" Width =" Auto" Grid.Column =" 0" MaxWidth =" 100" > Overwrite < /Button > < Button HorizontalAlignment =" Center" Width =" Auto" Grid.Column =" 1" MaxWidth =" 100" > Skip < /Button > < Button HorizontalAlignment =" Right" Width =" Auto" Grid.Column =" 2" MaxWidth =" 100" > Cancel < /Button > < /Grid >

That just makes the buttons size to their content (rather than to available space), but it aligns them properly. If you inspect the above XAML in Kaxaml, you'll see what I mean. What I'm trying to achieve is having these buttons align properly AND grow up to 100 units wide. From what I've seen so far, resizing and aligning appear to be too closely linked in WPF to achieve this.

Any idea of how to both stretch a button up to a maximum width AND align it horizontally once it has reached that maximum width?

You're a flippin' genius! Don't know why I didn't think of that. Here's what I (read: you) came up with (and it works reasonably well):
< Grid HorizontalAlignment =" Stretch" > < Grid.ColumnDefinitions > < ColumnDefinition MaxWidth =" 100" Width =" *" / > < ColumnDefinition Width =" 0.01*" / > < ColumnDefinition MaxWidth =" 100" Width =" *" / > < ColumnDefinition Width =" 0.01*" / > < ColumnDefinition MaxWidth =" 100" Width =" *" / > < /Grid.ColumnDefinitions > < Button Grid.Column =" 0" > b1 < /Button > < Button Grid.Column =" 2" > b2 < /Button > < Button Grid.Column =" 4" > b3 < /Button > < /Grid >
I had to set the width of the other columns to some small proportion because "Auto" was causing the grid to collapse those columns and a value of "1*" was causing the buttons to shrink prematurely. Thanks again, great idea!
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •