Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this WPF button stretch across the window?
    primarykey
    data
    text
    <p>The button below always expands to be as wide as the TextBlock. I've tried StackPanel, DockPanel, Width="Auto", etc.</p> <p>How can I make the button expand to the <strong>size of its own text</strong> (as in HTML) and not to the size of text in its environement? </p> <pre><code> &lt;DockPanel HorizontalAlignment="Left"&gt; &lt;Button x:Name="ButtonFavorite" DockPanel.Dock="Top" Content="Customers" Margin="10" Width="Auto" Click="ButtonFavorite_Click"&gt; &lt;/Button&gt; &lt;TextBlock DockPanel.Dock="Top" Text="this is a long text which makes the button stretch across the window, if this text is just a couple words, the button will be smaller, and this drives me up the wall" Margin="10" TextWrapping="Wrap" /&gt; &lt;/DockPanel&gt; </code></pre> <h1>ANSWER:</h1> <p>Thanks Greg, that did it. Here is the full XAML that works now, you can right-click the button to change its Content so see that the button expands and contracts appropriately.</p> <pre><code>&lt;Window x:Class="Test3784234.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;DockPanel HorizontalAlignment="Left"&gt; &lt;StackPanel DockPanel.Dock="Top" Orientation="Horizontal" &gt; &lt;Button x:Name="ButtonFavorite" Padding="5" Cursor="Hand" DockPanel.Dock="Top" Content="Customers" Margin="10" Click="ButtonFavorite_Click"&gt; &lt;Button.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem x:Name="menuItemReports" Header="Reports" Click="MenuItem_Click" /&gt; &lt;MenuItem x:Name="menuItemContracts" Header="Contracts" Click="MenuItem_Click"/&gt; &lt;MenuItem x:Name="menuItemCustomers" Header="Customers" Click="MenuItem_Click" /&gt; &lt;MenuItem x:Name="menuItemDocumentation" Header="Documentation Creation Instructions" Click="MenuItem_Click" /&gt; &lt;MenuItem x:Name="menuItemEmail" Header="E-Mail" Click="MenuItem_Click" /&gt; &lt;/ContextMenu&gt; &lt;/Button.ContextMenu&gt; &lt;/Button&gt; &lt;/StackPanel&gt; &lt;TextBlock x:Name="TheMessage" DockPanel.Dock="Top" Text="Right-click the 'favorites' button to change its function." Margin="10" TextWrapping="Wrap"/&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload