Setting WPF Button background to text - wpf

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>

Related

WPF c# a button with a textbox

I need to create a button similar like below, it is a textbox and a button combined, you can click "Get Code" button and you will receive a SMS text with the code, then you can input the code in the textbox.
textbox and a button
anyone can help?
Hi, my current code is quite simple.
<Button
Grid.Row="2"
Grid.Column="0"
HorizontalOptions="Center"
Text="Get Code"
FontFamily="{ StaticResource IconsFontFamily }"
Style="{ StaticResource CircleActionButtonFlatStyle }"
micro:Message.Attach="[Event Clicked] = [Action SendVerifyCodeAsync()]"/>
<Entry
Grid.Column="1"
Grid.Row="2"
Grid.ColumnSpan="2"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand"
Text="{Binding Code}"
Placeholder="Send Code" />
Has people said in comments, you need to wrap it in borders.
With this example, you just need to change the color for the ones you want, and then you can set Click events in the respective borders to make them act like buttons and do the respective actions. You can add the code micro:Message.Attach="[Event Clicked] = [Action SendVerifyCodeAsync()]" to the respective border.
Code:
<Border BorderBrush="Black" BorderThickness="1" Margin="10" CornerRadius="15">
<Grid Margin="-1">
<TextBox TextWrapping="Wrap" Text="Input code" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" CornerRadius="15" Background="#FF39D3D3">
<TextBlock TextWrapping="Wrap" Text="Get Code" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0"/>
</Border>
</Grid>
</Border>
Example:

WPF TextBox will not fill if DockPanel.LastChildFill=true

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>

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/

Align label to the middle of dock panel

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

Resources