Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Dockpanel first child uses remaining space

In a window I have there is a list of DockPanels to specify a couple of files. Each DockPanel has a TextBox (for the path) and a button (to browse for a file).

I've recreated a simple WPF page to demostrate the problem here:

<Page     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Width="150"     Height="22">     <DockPanel>         <TextBox HorizontalAlignment="Stretch"/> <!-- path to file -->         <Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->     </DockPanel> </Page> 

The problem is that I want the button to the right of the textbox, but that causes the textbox to be really small since the LastChild of the DockPanel is the button it uses up the remaining space. Ive tried to change this by shuffling them around and setting LastChildFill="False" but that only causes the button to be small again, not making the TextBox wide (even with HorizontalAlignment="Stretch").

The reason I want the controls in that order is I want the user to reach the TextBox before the Button when using tab to navigate in the window. I looked into setting TabIndex but it feels hacky, favorite features of WPF is that tabindex are in the order the conrols are defined in the XAML. Not to mention that I probably would have to manually set TabIndex on everything in the Window.

To me, it seems that the setting of TextBox.HorizontalAlignment isn't respected. How can I make the first control use as much space as it can but still preserve tab order?

like image 505
Mizipzor Avatar asked Sep 09 '10 15:09

Mizipzor


2 Answers

Make it like this:

<DockPanel LastChildFill="True">     <Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->     <TextBox DockPanel.Dock="Left" HorizontalAlignment="Stretch"/> <!-- path to file --> </DockPanel> 
like image 51
Jonathan Barez Avatar answered Sep 20 '22 23:09

Jonathan Barez


If you don't want the behavior of DockPanel, don't use a DockPanel.

<Grid>     <Grid.ColumnDefinitions>         <ColumnDefinition/>         <ColumnDefinition Width="Auto"/>     </Grid.ColumnDefinitions>      <TextBox />     <Button Content="..." Grid.Column="1"/> </Grid> 
like image 32
John Bowen Avatar answered Sep 19 '22 23:09

John Bowen