您的位置:首页 > 移动开发 > Objective-C

Wpf usercontrol remove/add

2009-09-27 22:02 405 查看
 做一下笔记明天上班赶紧补上!

今天在公司快下班了发现问题。

不知道如何去删除usercontrol 从窗体中。郁闷!

记下来实现方式:

定义一个类f_BaseControl 继承 UserControl

里面定义 事件和委托 ,和执行事件的方法

 

public class f_BaseControl : UserControl
{
public delegate void dffffff();
public event dffffff ff;
public void ffd()
{
if (ff != null)
{
ff();
}
}

}


用户控件 UserControl1 继承 UserControl 里面添加一个button

 

 
<local:f_BaseControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"  xmlns:lo="clr-namespace:WpfApplication1">
<Grid>
<Button Height="49" Margin="76,90,82,0" Click="button1_Click" Name="button1" VerticalAlignment="Top">Button</Button>
</Grid>
</local:f_BaseControl>


 

button 事件 调用父类 f_BaseControl  的方法

 

private void button1_Click(object sender, RoutedEventArgs e)
{
this.ffd();
}


 

 

最后在窗体中添加一个容器。

动态将 UserControl1 添加到容器中

 

 

 

 
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
Type t = typeof(Window1).Assembly.GetType("WpfApplication1.UserControl1",false);
f_BaseControl u = Activator.CreateInstance(t) as f_BaseControl;
u.ff += new f_BaseControl.dffffff(u_ff);//添加事件
this.UserPenal.Children.Clear();
this.UserPenal.Children.Add(u);
}

void u_ff()
{
this.UserPenal.Children.Clear();//清除了
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wpf button class object null