Wpf usercontrol remove/add
2009-09-27 22:02
405 查看
做一下笔记明天上班赶紧补上!
今天在公司快下班了发现问题。
不知道如何去删除usercontrol 从窗体中。郁闷!
记下来实现方式:
定义一个类f_BaseControl 继承 UserControl
里面定义 事件和委托 ,和执行事件的方法
用户控件 UserControl1 继承 UserControl 里面添加一个button
button 事件 调用父类 f_BaseControl 的方法
最后在窗体中添加一个容器。
动态将 UserControl1 添加到容器中
今天在公司快下班了发现问题。
不知道如何去删除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();//清除了 } }
相关文章推荐
- ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同
- Eclipse User Library Add/Edit/Remove
- How to add remove user (standard user/non-root) in Kali Linux?
- [WPF] UserControl vs CustomControl
- WinForms Controls > Examples > How to: Add New XtraForm, RibbonForm and XtraUserControl to Your Proj
- Wpf UserControl 截图
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
- WPF中用户控件对比自定义控件(UserControl VS CustomControl)
- Wpf UserControl使用 KeyBinding,失效问题
- WPF UserControl和ContentControl的区别
- WPF之路——用户控件对比自定义控件(UserControl VS CustomControl)
- [WPF] UserControl vs CustomControl
- WPF UserControl事件传递
- ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同
- addChildViewController和removeFromParentViewControl
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
- Nibblestutotials.net教程 – WPF系列之User Control
- Q111090: BUG: ARPSYSTEMCOMPONENT Will Not Hide Project from Control Panel's Add/Remove Programs Applet
- WPF UserControl弹出子窗口切换界面时消失
- [WPF] UserControl vs CustomControl