In a window I have there is a list of DockPanel
s 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?
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With