C# 在 WPF 4.5 中以编程方式向 Grid RowDefition 添加控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12468658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically add control to Grid RowDefition in WPF 4.5
提问by Dominik
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.
我一直在浏览这个网站(和许多其他网站),试图弄清楚发生了什么以及为什么这不起作用。我正在运行 VS2012 并创建了一个 WPF C# 应用程序(目标 .NET 4.5)。我是 WPF 的新手,花了很多年时间编写 Windows 窗体应用程序,但决定冒险尝试,到目前为止我喜欢 XAML。
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
最终我想:1) 在我的网格中删除特定行 (RowDefinition) 中的用户控件 2) 在该特定行中放置另一个用户控件
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:
但是我似乎无法放置一个简单的按钮控件。我想要做的是在第 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>
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.
我可以看到(在我的手表中)每次单击第一个按钮时 gridMain.Children 计数值都会增加,但屏幕上没有任何显示。
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!
提前致谢!
采纳答案by Kendall Frey
<ColumnDefinition Width="0"/>
Maybe you could see the button if you increased the width.
如果您增加宽度,也许您可以看到按钮。

