Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add control to Grid RowDefition in WPF 4.5

I've been through this site (and many others) trying to figure out what is going on and why this does not work. I am running VS2012 and have created a WPF C# app (target .NET 4.5). I am new to WPF, having spent many years coding Windows Forms apps, but decided to take the plunge, and am liking XAML so far.

Ultimately I want to: 1) Remove a user control in a particular Row (RowDefinition) in my Grid 2) Place another user control in that particular Row

However I can't seem to place even a simple button control. What I want to do is place a button in Row 4 (3 with zero index). Here is my XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
x:Class="TestApp2_WindowsClient.MainWindow"
Title="Test App 2" Height="700" Width="1000" MinHeight="700" MinWidth="1000" MaxHeight="700" MaxWidth="1000" FontSize="12" FontFamily="Segoe UI Semibold">
<Grid VerticalAlignment="Top" Name="gridMain">
    <Grid.RowDefinitions>
        <RowDefinition Height="60"/>
        <RowDefinition Height="152"/>
        <RowDefinition Height="240"/>
        <RowDefinition Height="60"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0"/>
    </Grid.ColumnDefinitions>

<StackPanel Name="stackButtons1" HorizontalAlignment="Left" Height="50" Margin="4,4,-310,4" Grid.Row="0" VerticalAlignment="Top" Width="300" Orientation="Horizontal" >
        <Button Content="Show Bookings" Height="24" Margin="4,0,0,0" Click="Button_Click_1" />
        <Button Content="Show Radio Buttons" Height="24" Margin="4,0,0,0" Click="Button_Click_2" />
    </StackPanel>

</Grid>
</Window>

The button code (first button in Stack Panel) is:

Button MyControl = new Button();
MyControl.Content = "Test Button!";

Grid.SetRow(MyControl, 3); 
Grid.SetColumn(MyControl, 0); 
gridMain.Children.Add(MyControl); 

I can see (in my watch) that the gridMain.Children count value increases every time I click the first button, but nothing appears on the screen.

It's probably something really silly, but a few hours of searching and trying a stack of different code has not helped.

Thanks in advance!

like image 557
Dominik Avatar asked Sep 17 '12 23:09

Dominik


1 Answers

<ColumnDefinition Width="0"/>

Maybe you could see the button if you increased the width.

like image 96
Kendall Frey Avatar answered Oct 12 '22 22:10

Kendall Frey