更新时间:2023-12-06 15:28:04
Hello Ragamoffyn。
添加
UserControl 您将创建它的新实例,设置任何必要的属性,然后将其添加到容器中。 像这样......
使用System.Windows;
名称空间WpfApplication4
{
公共部分类MainWindow:Window
{
public MainWindow()
{
this.InitializeComponent ();
}
private void myButton_Click(object sender,System.Windows.RoutedEventArgs e)
{
//在此示例中,每次单击按钮时都会添加对myGrid的控制。
//所以你可能想要在添加它之前清除网格的子节点。
myGrid.Children.Clear();
//创建一个新的控件实例。
myUserControl uc = new myUserControl();
//如果需要,设置控件的属性。
uc.Width = uc.Height = 300;
uc.HorizontalAlignment = HorizontalAlignment.Center;
uc.VerticalAlignment = VerticalAlignment.Center;
//添加控件。
myGrid.Children.Add(uc);
}
}
}MainWindow的XAML ...
< Window
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
x:Class =" WpfApplication4.MainWindow"
x:Name =" Window"
Title =" MainWindow"
Width =" 640"高度= QUOT; 480">
<网格x:名称=" LayoutRoot">
< Button x:Name =" myButton"含量="按钮和QUOT;的HorizontalAlignment = QUOT;左" VerticalAlignment = QUOT;陀螺"宽度= QUOT; 75"点击= QUOT; myButton_Click" />
< Grid x:Name =" myGrid"余量= QUOT; 79,0,0,0" />
< / Grid>
< / Window>希望能让你入门。
~Christine
Hi, all. A bit new to Blend here.
I'm trying to figure out how to add an instance of a UserControl during runtime in response to some event -- for example, clicking a button to produce an instance of some other control. Unfortunately, I can't find examples showing me how to do that, nor can I find any relevant documentation. The closest thing I've found is some short reference in this forum to an ".add" method, but I haven't been able to find any info on how to use that method.
Could one of you perhaps point me toward an example of such activity that I could reverse engineer, or maybe some documentation? Or if you have time and interest, some quick instructions...?
Thanks much for any help.
Hello Ragamoffyn.
To add a UserControl you would create a new instance of it, set any necessary properties, and then add it to a container. Like so...
using System.Windows; namespace WpfApplication4 { public partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); } private void myButton_Click(object sender, System.Windows.RoutedEventArgs e) { //In this example every time you click the button it will add the control to myGrid. //So you may want to clear the grid's children before adding it. myGrid.Children.Clear(); //Create a new instance of the control. myUserControl uc = new myUserControl(); //Set properties for the control if needed. uc.Width = uc.Height = 300; uc.HorizontalAlignment = HorizontalAlignment.Center; uc.VerticalAlignment = VerticalAlignment.Center; //Add the control. myGrid.Children.Add(uc); } } }And the XAML for the MainWindow...
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication4.MainWindow" x:Name="Window" Title="MainWindow" Width="640" Height="480"> <Grid x:Name="LayoutRoot"> <Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="myButton_Click"/> <Grid x:Name="myGrid" Margin="79,0,0,0"/> </Grid> </Window>Hope that gets you started.
~Christine