以编程方式将控件添加到WPF 4.5中的Grid RowDefition

Dom*_*nik 17 .net c# wpf xaml .net-4.5

我经历过这个网站(以及其他许多网站)试图弄清楚发生了什么以及为什么这不起作用.我正在运行VS2012并创建了一个WPF C#应用程序(目标.NET 4.5).我是WPF的新手,花了很多年编写Windows Forms应用程序,但决定冒险尝试,并且我喜欢XAML到目前为止.

最终我想:1)删除网格中特定行(RowDefinition)中的用户控件2)将另一个用户控件放在该特定行中

但是,我似乎无法放置一个简单的按钮控件.我想要做的是在第4行放置一个按钮(3个零索引).这是我的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>
Run Code Online (Sandbox Code Playgroud)

按钮代码(堆栈面板中的第一个按钮)是:

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

Grid.SetRow(MyControl, 3); 
Grid.SetColumn(MyControl, 0); 
gridMain.Children.Add(MyControl); 
Run Code Online (Sandbox Code Playgroud)

我可以看到(在我看来)每次单击第一个按钮时gridMain.Children计数值都会增加,但屏幕上没有任何内容.

这可能是非常愚蠢的事情,但是几个小时的搜索和尝试一堆不同的代码并没有帮助.

提前致谢!

Ken*_*rey 24

<ColumnDefinition Width="0"/>
Run Code Online (Sandbox Code Playgroud)

如果增加宽度,也许你可以看到按钮.