Align label to the middle of dock panel - wpf

I have a dock panel, with one label in the middle and another button on the far right.
Because of the button the label cannot align to the middle when the windows is maximized.
WPF:
<DockPanel Height="40" HorizontalAlignment="Stretch" Margin="-1,-2,0,0" Name="dockPanel1" VerticalAlignment="Stretch" Width="Auto" OpacityMask="{x:Null}">
<Label FontSize="18" Content="Sales" FontWeight="Bold" FontFamily="Arial" Width="883" Height="42" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Foreground="White" DockPanel.Dock="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"></Label>
<Button FontSize="18" Height="47" Width="123" Name="btnStart" Foreground="White" BorderBrush="{x:Null}" FlowDirection="LeftToRight" HorizontalContentAlignment="Center" FontFamily="Arial Rounded MT" ClickMode="Press" DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center" Padding="0" Content="Start" BorderThickness="0" Focusable="False">
</DockPanel>

Use a Grid instead of a DockPanel
Grid's allow objects to be placed on top of each other, so you can position your Label in the middle and the Button on the Right
<Grid>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" />
<Button HorizontalAlignment="Right" />
</Grid>
Also if you're new to WPF's Layouts, I'd recommend reading through WPF Layouts: A Quick Visual Start so you know what layouts are available and can pick the best one for your sitaution

Related

WPF left button, centered text, right button in a custom control

I'm trying to prototype a new custom control that has a left button, text, and right button.
The XAML looks like this:
<StackPanel Width="250" Orientation="Vertical" >
<DockPanel Background="Azure" Width="250">
<Button Name="btnLeft" HorizontalAlignment="Left" Width="30" Background="DarkGray" Click="btnAdd_Click" Template="{DynamicResource StandardButtonTemplate1}">
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Previous25px.png" VerticalAlignment="Center" Name="imgLeft"></Image>
</StackPanel>
</Button>
<TextBlock Name="tblkModelType" HorizontalAlignment="Center" Foreground="Black" FontSize="20">Import</TextBlock>
<Button Name="btnRight" HorizontalAlignment="Right" Width="30" Background="DarkGray" Click="btnAdd_Click" Template="{DynamicResource StandardButtonTemplate1}" >
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Next25px.png" VerticalAlignment="Center" Name="imgRight"></Image>
</StackPanel>
</Button>
</DockPanel>
</StackPanel>
The buttons are left and right justified ok, but the (tblkModelType) is left justified right up next to the left button. It doesn't appear that HorizontalAlignment on that tag does anything. If I remove the right button, the TextBlock does center.
Anyone know how to get the buttons on the outside and the TextBlock centered in the Dockpanel? I guess I could use a grid, but it would make the rest of the control more complex.
This should work:
<DockPanel Background="Azure" Width="250" >
<Button DockPanel.Dock="Left" Name="btnLeft" Width="30" Background="DarkGray" Click="btnAdd_Click" Template="{DynamicResource StandardButtonTemplate1}">
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Previous25px.png" VerticalAlignment="Center" Name="imgLeft"></Image>
</StackPanel>
</Button>
<Button DockPanel.Dock="Right" Name="btnRight" Width="30" Background="DarkGray" Click="btnAdd_Click" Template="{DynamicResource StandardButtonTemplate1}" >
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Next25px.png" VerticalAlignment="Center" Name="imgRight"></Image>
</StackPanel>
</Button>
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Center" Name="tblkModelType" Foreground="Black" FontSize="20">Import</TextBlock>
</DockPanel>
Use DockPanel.Dock to dock btnLeft to the left of the DockPanel, btnRight to the right. Then for tblkModelType set DockPanel.Dock="Top". This will stretch the TextBlock to the entire width of the DockPanel between buttons.
This works for me. As you said your control is more complex, so in case of any problem share more xaml with us.

Viewbox TabControl sizing gets messed up depending on font size

I have a program that lets you edit different properties so that you can edit a different control.
I'm trying to get the layout right for the editor but I can't seem to get it easily.
I have put it in a viewbox so it sizes according to the screen. This is the XAML for the editor:
<Viewbox Grid.Column="0" Grid.Row="2" VerticalAlignment="Top" HorizontalAlignment="Center">
<TabControl Background="{DynamicResource CustomOrange}">
<TabItem Header="Background" Visibility="Visible" FontSize="10">
<StackPanel>
<DockPanel LastChildFill="False">
<Label Content="Background" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30"/>
</DockPanel>
<DockPanel LastChildFill="False" Margin="2">
<Label Content="Background Image" DockPanel.Dock="Left"/>
<Button Content="Find Image" DockPanel.Dock="Right"/>
</DockPanel>
<DockPanel LastChildFill="False" Margin="2">
<Label Content="Show Text" DockPanel.Dock="Left"/>
<CheckBox Content="Find Image" DockPanel.Dock="Right" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DockPanel>
</StackPanel>
</TabItem>
<TabItem Header="Button" Visibility="Visible">
<StackPanel>
<DockPanel LastChildFill="False">
<Label Content="Background IMAGE" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30"/>
</DockPanel>
<DockPanel LastChildFill="False" Margin="2">
<Label Content="Background Image" DockPanel.Dock="Left"/>
<Button Content="Find Image" DockPanel.Dock="Right"/>
</DockPanel>
<DockPanel LastChildFill="False" Margin="2">
<Label Content="Show Text" DockPanel.Dock="Left"/>
<CheckBox Content="Find Image" DockPanel.Dock="Right" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DockPanel>
</StackPanel>
</TabItem>
</TabControl>
</Viewbox>
The problem is that the first TabItem looks like this:
But because I have changed the content of the label, the second TabItem looks like this:
Now, I know there are simpler ways to do this, but the problem is I don't know for sure how many options I am implementing for the user (Width, Height, Location, Font, Background, Content etc).
I have done it before with a grid but the problem is anytime I remove an option then all of them need to be re-done so that they are all in the exact Grid.Row which is why I am using StackPanel for the rows, and I am using the DockPanel so they keep separated horizontally
Thank you!!!

Setting WPF Button background to text

I'd like to have a button with text at the bottom, but more text in the background, specifically from a UI font. That way the content text at the bottom can be seen over the background.
It doesn't seem like StackPanels and DockPanels help as they always allocate room for each control so they don't overlap.
Something like:
<Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Bottom" FontFamily="Segoe UI Symbol">
<Button.Background>
<TextBox Text="" Foreground="White" Background="#FF5B9BD5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" BorderThickness="0"/>
</Button.Background>
<TextBox Text="Text" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" BorderThickness="0"/>
</Button>
This code does not work as the Background does not support controls, only Brushes.
Using a grid achieves this:
<Button VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Foreground="White" FontFamily="Segoe UI Symbol" FontStretch="Expanded" BorderThickness="0">
<Grid>
<TextBox Text="" Foreground="White" Background="#FF5B9BD5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="36" BorderThickness="0"/>
<TextBox Text="Text" Foreground="White" Background="#FF5B9BD5" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" BorderThickness="0"/>
</Grid>
</Button>

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 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