在WPF 4.5中以编程方式向Grid RowDefition添加控件 [英] Programmatically add control to Grid RowDefition in WPF 4.5
问题描述
我一直在浏览该网站(以及许多其他网站),以弄清楚发生了什么以及为什么它不起作用。我正在运行VS2012,并创建了WPF C#应用程序(目标.NET 4.5)。我是WPF的新手,已经花了很多年编写Windows Forms应用程序的代码,但是决定尝试一下,并且到目前为止还很喜欢XAML。
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.
我最终想: b $ b 1)删除网格中特定行(RowDefinition)中的用户控件
2)在该特定行中放置另一个用户控件
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
但是我甚至无法放置一个简单的按钮控件。我想做的是在第4行(索引为零的3)中放置一个按钮。这是我的XAML:
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);
我可以看到(在我的手表中)每次单击gridMain.Children计数值都会增加第一个按钮,但屏幕上什么都没显示。
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.
预先感谢!
推荐答案
<ColumnDefinition Width="0"/>
如果您增加宽度,也许可以看到该按钮。
Maybe you could see the button if you increased the width.
这篇关于在WPF 4.5中以编程方式向Grid RowDefition添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!