WPF TextBox will not fill if DockPanel.LastChildFill=true - wpf

Why won't my TextBox fill the available space in its DockPanel parent? I expected it to stretch to fill the remaining horizontal space. The Button is attached to the right nicely. I've got this at the top of my Window:
<DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" >
<TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
<Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
</DockPanel>
The TextBox attaches to the left but is about 5 pixels wide. It's the last child, and has the defaults DockPanel.Dock=Left, HorizontalAlignment=Stretch. I've tried other dock and alignment values without success. Is TextBox an exception to the usual layout rules?

When you set DockPanel.LastChildFill=true
What is the last child you're adding?
The button.
Not the textbox.
Order is top (first) to bottom (last).
I'm not sure exactly what result you want but maybe you just need to make the textbox the last child:
<DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" >
<Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
<TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
</DockPanel>

This seems to work for me:
<DockPanel Margin="20,10,20,0" DockPanel.Dock="Top" LastChildFill="True">
<Button Content=" _Evaluate" IsDefault="True" HorizontalAlignment="Right" DockPanel.Dock="Right" />
<TextBox TextWrapping="Wrap" Text="{Binding Input}" Background="#FFE4EBFF" Margin="0,0,5,0" />
</DockPanel>

Related

Left and right alignment leaving space in the middle inside StackPanel

I have this user control:
And I want to achieve this result:
The XAML of the bottom part is like this:
<StackPanel HorizontalAlignment="left" Orientation="Horizontal">
<TextBox materialDesign:HintAssist.Hint="Filtrar por patente" Width="auto" Margin="5" Text="{Binding Filtro, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"></TextBox>
<Button Width="120" Style="{DynamicResource MaterialDesignRaisedButton}" Margin="5" HorizontalAlignment="Right" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}">Volver</Button>
</StackPanel>
I cannot find a way to align textbox to the left and button to the right, leaving a dynamic space in the middle. Button should have fixed width and textbox should be auto.
Thanks
Use a DockPanel. You'll want to stretch it instead of aligning it to the left.
<DockPanel
HorizontalAlignment="Stretch"
>
<TextBox
DockPanel.Dock="Left"
materialDesign:HintAssist.Hint="Filtrar por patente"
Width="auto"
Margin="5"
Text="{Binding Filtro, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left"
/>
<Button
DockPanel.Dock="Right"
Width="120"
Style="{DynamicResource MaterialDesignRaisedButton}"
Margin="5"
HorizontalAlignment="Right"
Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
>Volver</Button>
</DockPanel>

Unable to fill the height of TextBox in DockPanel

I am trying to fill the TextBox height for all remaining space in the DockPanel but no success. I have not even found a successful example code doing this stuff.
Here is what I have done:
<Grid x:Name="LayoutRoot">
<DockPanel Background="Red" LastChildFill="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Button DockPanel.Dock="Bottom" Content="Save" Width="100" Height="50" HorizontalAlignment="Right" Margin="10 5"/>
<TextBox AcceptsReturn="True"
AcceptsTab="True"
Margin="5 10"
TextWrapping="Wrap"/>
</DockPanel>
</Grid>
Am I missing something?? I have also tried VerticalAlignment property to Stretch of TextBox, no success. Below is the output window.
Try this,set your textbox style to NULL and check whether its working fine.
<DockPanel Background="Red" LastChildFill="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Button DockPanel.Dock="Bottom" Content="Save" Width="100" Height="50" HorizontalAlignment="Right" Margin="10 5"/>
<TextBox AcceptsReturn="True" AcceptsTab="True" Margin="5 10" TextWrapping="Wrap" Style="{x:Null}"/>
</DockPanel>
To get the textbox to autosize without having to mess with the Style you can simply set your height back to Double.NaN. The xaml below is stripped to only show the relevant bits.
<DockPanel LastChildFill="True">
<Button DockPanel.Dock="Bottom"/>
<TextBox Height="NaN"/>
</DockPanel>

WPF image and text alignment in button

I need a way to align a label to right and align an image to right. I tried this code:
<Button HorizontalAlignment="Left" Margin="11,265,0,0" VerticalAlignment="Top" Width="190" Height="51">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<Image Source="Resources/Accept-icon.png" Stretch="Uniform" HorizontalAlignment="Left"/>
<Label Content="ذخیره" HorizontalContentAlignment="Right" VerticalAlignment="Center" FontFamily="2 badr" FontSize="20"/>
</StackPanel>
</Button>
But I see label sticks to image.
Also is there any way to have some parameter like cell padding (from right/left/top/bottom)?
Try using a DockPanel instead
<Button HorizontalAlignment="Left" Margin="11,265,0,0" VerticalAlignment="Top" Width="190" Height="51">
<DockPanel HorizontalAlignment="Stretch">
<Image Source="Resources/Accept-icon.png" Stretch="Uniform" DockPanel.Dock="Left"/>
<Label Content="ذخیره" DockPanel.Dock="Right" VerticalAlignment="Center" FontFamily="2 badr" FontSize="20"/>
</DockPanel>
</Button>
For your padding question, which element are you trying to pad?
try using Grid instead of StackPanel
<Grid>
<Image ... HorizontalAlignment="Left"/>
<Label ... HorizontalAlignment="Right"/>
</Grid>
there is Padding property which is published by types like Block, Border, Control, and TextBlock so for example it will not be published by Image control, which inherits directly from FrameworkElement, but will be by Label which is a Control

Why is my dockpanel's LastChild overflowing its container?

I have a DockPanel with LastChildFill = true. It has 4 children: a StackPanel(top), another DockPanel(top), and three more StackPanels(bottom, left & none respectively). The last StackPanel ("ResultsPanel") has no DockPanel.Dock property set, nor does it have a width/height, yet when the databinding results in more rows than fit on the screen, it continues down behind the bottom-docked StackPanel. I would expect it to fill the center "hole" that I left between the other docked children.
If I leave the ScrollBarVisibilities set to Auto, it doesn't even show them. Any advice would be appreciated. Here is the pertinent code:
<DockPanel Name="JobsDock" LastChildFill="True">
<StackPanel DockPanel.Dock="Top" Height="40" >
<Label />
<Border CornerRadius="5" Width="90" Height="25" >
<Label Content="Classic View" />
</Border>
</StackPanel>
<DockPanel DockPanel.Dock="Top" Height="70" Name="SearchJobsPanel" >
<ComboBox Name="SearchOptionComboBox" VerticalAlignment="Bottom" Width="240" />
<StackPanel Height="50" Name="JobPanel" Width="90" Visibility="Collapsed" VerticalAlignment="Bottom" >
<Label Content="Job Number" HorizontalAlignment="Center" />
<TextBox Name="JobTextBox" />
</StackPanel>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Button Height="25" Name="SearchButton" Width="90">
<StackPanel Orientation="Horizontal">
<Image Stretch="Fill" Margin="0,0,5,0" />
<Label Content="Search" FontSize="10" VerticalContentAlignment="Top" />
</StackPanel>
</Button>
</StackPanel>
</DockPanel>
<StackPanel Name="FiltersPanel" DockPanel.Dock="Left" Width="120" Opacity="0" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Name="ResetFilterButton" >
<Image Source="Images/ResetFilter.png" Width="30" />
</Button>
<Button Name="ApplyFilterButton" >
<Image Width="30" />
</Button>
</StackPanel>
<Label Name="ProjectsLabel" />
<Label Name="TasksLabel" Margin="0,10,0,0" />
</StackPanel>
<StackPanel Name="ResultsPanel" Opacity="0">
<ListView x:Name="DisplayedJobListView" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListView.View>
<GridView>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
Jurgen, on the msdn forum answered this question for me. Here is his reply:
Hi,
the last (filling) DockPanel child is a StackPanel ("ResultsPanel"). A StackPanel's layout is never defined by containing controls (there is nothing working like VerticalLayout="Stretch" for a StackPanel). It will always take the space neccessary for it's contained controls to fully expand.
As you only have one child in that StackPanel (which has a scrolling mechanism of it's own) remove the StackPanel.
Hope this helps.
Cheers
Jürgen

Why won't the WPF progressbar stretch to fit?

This is my original code:
<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<ProgressBar Height="23" Name="searchProgressBar" Foreground="Blue" BorderBrush="#00000000" BorderThickness="1" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<TextBlock Text="asdf" Height="23" Name="progressTextBlock" VerticalAlignment="Top" Foreground="Red" HorizontalAlignment="Right"/>
</StackPanel>
The progressbar was very small, maybe 2 or 3 pixels wide, then there was the text block and empty space after. So I tried explicitly docking the elements to sides:
<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" >
<ProgressBar DockPanel.Dock="Left" Height="23" Name="searchProgressBar" Foreground="Blue" BorderBrush="#00000000" BorderThickness="1" VerticalAlignment="Top" />
<TextBlock DockPanel.Dock="Right" Text="asdf" Height="23" Name="progressTextBlock" VerticalAlignment="Top" Foreground="Red" HorizontalAlignment="Right"/>
</DockPanel>
No avail. I also tried modifying each solution by setting HorizontalAlignment="Stretch" on the progress bar, but there's no change. How do i stretch it to fill all the space there is after the text block has been rendered?
Remove DockPanel.Dock="Left" from the ProgressBar and switch the order of the controls:
<DockPanel>
<TextBlock DockPanel.Dock="Right" Height="23" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<ProgressBar Height="23" VerticalAlignment="Top" />
</DockPanel>
By default, DockPanel has its property LastChildFill set to true, which will make the ProgressBar take the available space.
ahh I see what you're trying to do. This is probably a better place to use a grid with 2 column definitions. The first(the left one) columndefinition with Width="*" and the second(the right one) with a width set to Width="Auto". For more info about auto vs * see http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9a7e6591-1fae-4295-b68a-be97e8e53d06/

Resources