Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a grid inside a button have 100 percent width in WPF?

Tags:

c#

button

wpf

grid

I have a button on a window that sizes to the window. I have put a grid inside the button with one row and two columns, and put a path in first column, and a textbox in the second.

My problem is I can't get the grid to stretch with the button.

Here is what is happening:

Here is what is happening:

Here is what I want to happen:

enter image description here

I have the grids HorizontalAlignment="Stretch", yet it is not stretching. What am I doing wrong here?

Here is the code:

    <Window x:Class="GridInButtonIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GridInButtonIssue"
        mc:Ignorable="d"
        Title="MainWindow" Height="136.5" Width="269.839">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button x:Name="btn_SelectMode" Grid.Row="0" Margin="0,35,0,0" >
            <Grid HorizontalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="6*" />
                    <ColumnDefinition Width="4*" />
                </Grid.ColumnDefinitions>
                <Canvas x:Name="svg2" Grid.Column="0" Width="25" Height="25" HorizontalAlignment="Center">
                    <Canvas.RenderTransform>
                        <TranslateTransform X="0" Y="0"/>
                    </Canvas.RenderTransform>
                    <Canvas x:Name="layer1">
                        <Canvas.RenderTransform>
                            <TranslateTransform X="-298.50531" Y="-576.33075"/>
                        </Canvas.RenderTransform>
                        <Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="298.6" Canvas.Top="576.4" Width="19.9" Height="19.9" x:Name="path4144" Fill="#FF951718" StrokeThickness="0.14452878" Stroke="#FFFD0000" StrokeMiterLimit="4" StrokeLineJoin="Miter" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Opacity="1"/>
                    </Canvas>
                </Canvas>
                <TextBlock Grid.Column="1" Text="Test Button" HorizontalAlignment="Left" />
            </Grid>
        </Button>
    </Grid>
</Window>
like image 607
user2109254 Avatar asked Feb 17 '16 09:02

user2109254


People also ask

How do I create a dynamic grid in WPF?

The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();

How do I show grid lines in WPF?

First Method: By typing XAML Code ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True.

What is the difference between grid and Stackpanel in WPF?

The difference is the way they contain elements. Elements in Grid are stored in matrix form or you can say tabular form. You can define columndefinitions and rowdefinitions and you can access and store element at a particular row and column. But in stackpanel, elements are stored in stack format .

What is grid RowSpan in WPF?

You can span across multiple rows and columns using the Grid. RowSpan and Grid. ColumnSpan attached properties. The default value for both these properties is 1. The Grid will attempt to assign as many row spans or column spans as it can up to the amount specified by the Grid.


1 Answers

You have to set the HorizontalContentAlignment to Strech on the Button directly like so:

<Window x:Class="GridInButtonIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GridInButtonIssue"
        mc:Ignorable="d"
        Title="MainWindow" Height="136.5" Width="269.839">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button x:Name="btn_SelectMode" Grid.Row="0" Margin="0,35,0,0" HorizontalContentAlignment="Stretch" >
      <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
          <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="6*" />
          <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>
        <Canvas x:Name="svg2" Grid.Column="0" Width="25" Height="25" HorizontalAlignment="Center">
          <Canvas.RenderTransform>
            <TranslateTransform X="0" Y="0"/>
          </Canvas.RenderTransform>
          <Canvas x:Name="layer1">
            <Canvas.RenderTransform>
              <TranslateTransform X="-298.50531" Y="-576.33075"/>
            </Canvas.RenderTransform>
            <Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="298.6" Canvas.Top="576.4" Width="19.9" Height="19.9" x:Name="path4144" Fill="#FF951718" StrokeThickness="0.14452878" Stroke="#FFFD0000" StrokeMiterLimit="4" StrokeLineJoin="Miter" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Opacity="1"/>
          </Canvas>
        </Canvas>
        <TextBlock Grid.Column="1" Text="Test Button" HorizontalAlignment="Left" />
      </Grid>
    </Button>
  </Grid>
</Window> 

So it looks like this:

Button with Content

like image 144
M.E. Avatar answered Sep 21 '22 11:09

M.E.