Align button content - wpf

I want the content of a wpf button to be left aligned
I tried the following but the text is still centered inside the button.
<Button >
<StackPanel HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="Save"/>
</StackPanel>
</Button>
What do i do?

Found it, it's HorizontalContentAlignment.
:)

You don't need the StackPanel or the TextBlock. All you need is
<Button HorizontalContentAlignment="Left" Content="Save" />

You can align in two way, Horizontal and Vertical
<Button Content="Export" VerticalContentAlignment="Bottom" name="MyBtn1" />
or
<Button Content="Export" HorizontalContentAlignment="Center" name="MyBtn2" />
See the follow image to view the possibilities settings:

Related

WPF Button tooltip just pops up for a blink of an eye

In my WPF application I have a datagrid and one of it's columns contains two buttons. The buttons should have a tooltip, which appears with a 500ms delay. Unfortunately, the Tooltip just pops up for a blink of an eye.
The button's code:
<Button Grid.Row="1" Grid.Column="1" Command="{Binding Path=Data.CreateSingleBrickLinkInvoiceCommand,
Source={StaticResource ProxyElement}}" ToolTip="{DynamicResource create_invoice_tooltip}"
ToolTipService.InitialShowDelay="500" >
<Image Source="/Images/download.png" />
</Button>
try use ToolTipService.ShowDuration Attached Property
<Button ToolTipService.ShowDuration="500" Grid.Row="1" Grid.Column="1" Command="{Binding Path=Data.CreateSingleBrickLinkInvoiceCommand,
Source={StaticResource ProxyElement}}" ToolTip="{DynamicResource create_invoice_tooltip}"
ToolTipService.InitialShowDelay="500" >
<Image Source="/Images/download.png" />
</Button>

Centering several buttons WPF

I was writing C# code in winforms. In winForms using this menu (in the picture) I make center several buttons. But I can not find out any way to make center some elements horizontally in WPF.
Use the HorizontalAlignment property. For example:
<StackPanel Orientation="Horizontal" Height="50" HorizontalAlignment="Center">
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
</StackPanel>

WPF Dockpanel won't align right

I have this code on which I use a DockPanel on a Button.Content. However it doesn't let me right align the last image (small arrow).
<Button Height="70"
HorizontalContentAlignment="Left">
<Button.Content>
<DockPanel LastChildFill="False">
<Image DockPanel.Dock="Left"
Height="50"
Width="50"
Source="{StaticResource Placeholder}"
Stretch="UniformToFill"
Margin="5" />
<TextBlock DockPanel.Dock="Left"
Text="Dummy text"
VerticalAlignment="Center"
Margin="5" />
<Image DockPanel.Dock="Right"
Height="24"
Width="24"
Source="{StaticResource Right_Arrow_24}"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Stretch="UniformToFill"
Margin="5" />
</DockPanel>
</Button.Content>
</Button>
It now gives me this:
So the right small arrow should be placed on the right of the button and not just after the TextBlock.
I've found some similar issues and it looks like I did it correct, but somehow it isn't..
What am I doing wrong here?
Try to set the HorizontalContentAlignment of your button to "Stretch". Otherwise your DockPanel will use the size need by its content, then align left. You can confirm this behaviour by using different text lengths for your TextBlocks
You simply have to append an extra child to the DockPanel (say an empty Canvas) without the DockPanel.Dock attribute, so that all the remaining space is assigned to it.
In general, DockPanel only works fine only when its last child has no Dock constraint
Try setting
HorizontalContentAlignment="Stretch"
On your button.

How to make a WPF TextBox fill all available space between two buttons ?

I'm trying to make a WPF TextBox fill all available space between two buttons. For some reason the code below does not what I'm trying to achieve
<DockPanel Height="48" LastChildFill="False">
<Button DockPanel.Dock="Left">
<Image Source="Images\large_load.png"></Image>
</Button>
<Button DockPanel.Dock="Left">
<Image Source="Images\large_reload.png"></Image>
</Button>
<TextBox Height="24" HorizontalAlignment="Stretch" DockPanel.Dock="Left"></TextBox>
<Button DockPanel.Dock="Right" Width="48">
<Image Source="Images\large_delete.png"></Image>
</Button>
</DockPanel>
The TextBox is not stretched.
Another problem is that when text is added, the textbox width increases and eventually it pushes the right button out of the visible space.
Don't set LastChildFill to false and make the TextBox the last child (by moving the element to the bottom in the code).
(Or use a proper control, like a Grid)

How to dynamically add controls to a Silverlight Button?

I'm working on a Silverlight Polling control. My goal is to have a Button that contains other controls, so that when a user clicks on this combination, stuff happens.
Here's the basic XAML:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
<StackPanel Grid.Row="1" Grid.Column="0" Height="Auto" Width="Auto" Style="{StaticResource StackPnl}">
<TextBox Text="this is a text box" Style="{StaticResource Answer}" />
<ProgressBar Style="{StaticResource PollProgress}" />
</StackPanel>
</Button>
That works fine, but assume I don't have the StackPanel, etc, defined:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
</Button>
But that I want to add the StackPanel, TextBox, and ProgressBar at runtime.
Looking at just the first hurdle that I've run into, how do I dynamically add the StackPanel to the Button control?
Alternatively, if someone knows of a better way to do this...
var panel = new StackPanel();
Button.Content = panel;

Resources