Equal sized columns when Grid aligns to the right - wpf

I'm creating a Dialog in WPF with pretty standard Save and Cancel buttons. I want the Save and Cancel buttons to have the same width. I thought this would be a trivial matter. I placed the buttons in a Grid, with two columns sized to '*', yet the columns have different widths!!
<Grid Grid.Row="2" HorizontalAlignment="Right">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="Save" Margin="8" />
<Button Content="Cancel" Margin="8" Grid.Column="1" />
</Grid>
If I take off the Horizontal alignment, the columns size equally, but the Grid is too large. How do I get a Grid, aligned right, with two equal columns??

try
<UniformGrid HorizontalAlignment="Right" Columns="2">
<Button Content="Save" Margin="8" />
<Button Content="Cancel" Margin="8" Grid.Column="1" />
</UniformGrid>

Related

buttons are being cut off of the page

I have two sets of controls within a row. They are both in their respective stack panels. The first is aligned to the left and the second is aligned to the right. It looks good, except that the controls on the right are cut off by the application window. I can stretch out the window with my mouse and it looks fine, but it would be better if the buttons would just push in as the application window collapses.
Here is the xaml:
<Grid Grid.Row="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0"
Orientation="Horizontal"
HorizontalAlignment="Left">
<Label Content="Instant Message Text"
Margin="5"></Label>
<TextBox x:Name="txt_Message"
Width="400"></TextBox>
<Button Name="btn_instantMessage"
Content="Send Message"></Button>
<Label Content="Send To All Zones"></Label>
<CheckBox Name="chk_sentMessageToAllZones"
VerticalAlignment="Center"
HorizontalAlignment="Right"></CheckBox>
</StackPanel>
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left" >
<Button Content="Stop All Ads"
Margin="5"
Name="btn_stop"></Button>
<Button Content="Play All Ads"
Margin="5"
Name="btn_play"></Button>
</StackPanel>
</Grid>
Here is a screen shot of the damage. You can see how the button "Play All Ads" is getting cut off.
You should set SizeToContent property on your window/UC.
SizeToContent="Width"

WPF: why is the button stays on the left side?

I have this WPF button in my StackPanel:
<StackPanel Grid.ColumnSpan="6" Grid.Row="12" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
<Button x:Name="btnSave" Click="btnSave_Click" Content="{x:Static res:Strings.ToolPanelEditView_Button_Save}" HorizontalAlignment="Right" />
</StackPanel>
For some reason, the button shows on the left although I set its HorizontalAlignment to Right:
How can I make my save button show on the right?
P.S. when I change the StackPanel's HorizontalAlignment to Right instead of Stretch, the button does show on the right like it should (but then the gray background of the StackPanel does not stretch either...
I think that a Grid is more appropriate for your case
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnSave" Grid.Column="1" Content="Save" HorizontalAlignment="Right" />
<!--Your Other Button Grid.Column="2"-->
</Grid>
or you could as well replace the stackPanel with a DockPanel
You can use DockPanel as container, stretch it and dock your button to the right side
<DockPanel Grid.ColumnSpan="6" Grid.Row="12" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="LightGray" Height="40">
<Button Content="Save" DockPanel.Dock="Right"/>
<Button Content="Cancel" DockPanel.Dock="Right"/>
</DockPanel>

Relative positioning in WPF xaml

Is it possible to position a control in the XAML definiton relative to another control?
for example if I want to place a control exactly in the middle of another wider control.
and if so - how?
If you want to center the controls you could wrap them in a grid:
<Grid>
<TextBox Height="132" Width="229" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Button Content="Button" Height="23" Width="75" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
Other positioning can be accomplished by other panels like StackPanel etc. Grid beeing the most flexible choice.
Edit: updated with grid and columns for controlling position:
<Grid Height="50" Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="75*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Background="Red" />
<Button Content="Button" Grid.Column="2" />
</Grid>

automatic alignment of buttons

I got 4 buttons aligned inside a grid. Is there any way to make alignment automatic so that if one button's visibility is set to false the remaining buttons uses the space wisely[other buttons moves to accommodate into new space]?
Instead of a grid, put the buttons in a StackPanel with Orientation="Horizontal". Also, make sure you set your button Visibility to "Collapsed" instead of "Hidden".
Try the following:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal">
<Button Content="1" Width="50" Height="23" Visibility="Collapsed"/>
<Button Content="2" Width="50" Height="23"/>
<Button Content="3" Width="50" Height="23" Visibility="Collapsed"/>
<Button Content="4" Width="50" Height="23"/>
</StackPanel>
</Grid>
Or this:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="1" Content="1" Width="50" Height="23" Visibility="Collapsed"/>
<Button Grid.Column="2" Content="2" Width="50" Height="23"/>
<Button Grid.Column="3" Content="3" Width="50" Height="23" Visibility="Collapsed"/>
<Button Grid.Column="4" Content="4" Width="50" Height="23"/>
</Grid>
You should see the buttons centered but only show the second and fourth button
Depends on the layout and how complex you want the space distribution to be, if a uniform distribution is enough you could use a UniformGrid, in a normal Grid that would be a bit tricky as the columns and rows need to be adjusted.

Silverlight - Auto-resize textbox to fill up empty space

Ok this is what I wish to do.
I have a resizeable window which has 3 controls in same row in flowing order: textBlock, textBox and button.
textBlock and button have dynamic texts. So theirs size depends upon text inside.
Now what I wish to do is that textBox in the middle is always filling up all empty space between textBlock and button.
How do I do that?
I tried with the following code but it doesn't work because of fixed width in 1. and 3. column.
<Grid Margin="0,0,5,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" HorizontalAlignment="Left" Text="Text1"/>
<TextBox Grid.Column="1"/>
<Button Grid.Column="2" Content="Button1" HorizontalAlignment="Center"/>
</Grid>
You can use Auto for the two outer column widths instead of specifying the width
<Grid Margin="0,0,5,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Text1" />
<TextBox Grid.Column="1"/>
<Button Grid.Column="2" Content="Button1" />
</Grid>
You probably don't need the HorizontalAlignment in the columns either

Resources