我浏览了这个网站(和许多其他网站),试图找出原因和解决方法,但都没有成功。我正在运行VS2012,并创建了一个WPF C#应用程序(目标为.NET 4.5)。我是WPF的新手,之前一直编写Windows Forms应用程序,但决定尝试一下,到目前为止我很喜欢XAML。
我的最终目标是: 1)从我的Grid中删除特定行(RowDefinition)中的一个用户控件。 2)在该特定行中放置另一个用户控件。
然而,我甚至无法放置一个简单的按钮控件。我想要在第四行(索引为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>
按钮代码(Stack Panel 中的第一个按钮)如下:
Button MyControl = new Button();
MyControl.Content = "Test Button!";
Grid.SetRow(MyControl, 3);
Grid.SetColumn(MyControl, 0);
gridMain.Children.Add(MyControl);
我可以看到(在我的手表上)每次点击第一个按钮时,gridMain.Children计数值会增加,但屏幕上没有显示任何内容。
这可能是一些非常愚蠢的问题,但经过几个小时的搜索和尝试了一堆不同的代码之后,仍然没有帮助解决问题。
提前感谢!