在 WPF 4.5 中以编程方式向 Grid RowDefition 添加控件 [英] Programmatically add control to Grid RowDefition in WPF 4.5

查看:30
本文介绍了在 WPF 4.5 中以编程方式向 Grid RowDefition 添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览过这个网站(以及许多其他网站),试图弄清楚发生了什么以及为什么这不起作用.我正在运行 VS2012 并创建了一个 WPF C# 应用程序(目标 .NET 4.5).我是 WPF 的新手,多年来一直在编写 Windows 窗体应用程序,但我决定冒险一试,到目前为止我喜欢 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.

最终我想:1) 在我的 Grid 中删除特定行 (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆