WPF控件开发的入门引导
2010-04-05 21:17
393 查看
![](http://hi.csdn.net/attachment/201004/5/2008458_1270473506WYi4.gif)
如果动态创建WPF控件,动态捕捉控件的事件,以及定义属性等,上图是我学习后的一个效果图,后续会把实现的代码直接贴上来,我相信看代码是众网友的强项。
以下是我的工程文件预览:
![](http://hi.csdn.net/attachment/201004/5/2008458_12704735993A3v.gif)
App.xaml没动过,是系统生成的。
MainWindow.xaml
[xaml代码]
<Window x:Class="WpfMainApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grdParent">
<Button Content="Dynamic Load Wpf Control" Height="36" HorizontalAlignment="Left" Margin="12,263,0,0" Name="button1" VerticalAlignment="Top" Width="228" Click="button1_Click" />
<Button Content="Set Wpf Control Content" Height="29" HorizontalAlignment="Left" Margin="254,12,0,0" Name="button2" VerticalAlignment="Top" Width="213" Click="button2_Click" />
<TextBox Height="29" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="147" />
</Grid>
</Window>
[CS代码]
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Name="EditPlusControl"> <Grid Name="grdRoot"></Grid> </UserControl> [CS代码] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfComctl { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class EditPlus : UserControl { public event EventHandler ButtonClick; public event EventHandler NodeClick; public EditPlus() { InitializeComponent(); Button btnItem = new Button(); btnItem.Content = "ClickMe"; btnItem.Click += new RoutedEventHandler(btnItem_Click); btnItem.Width = 100; btnItem.Height = 20; Thickness loc = new Thickness(); loc.Left = 0; loc.Top = 0; btnItem.Margin = loc; grdRoot.Children.Add(btnItem); TreeView treeView1 = new TreeView(); treeView1.MouseDown += new MouseButtonEventHandler(treeView1_MouseDown); treeView1.Height = 100; treeView1.Width = 60; Thickness loc1 = new Thickness(); loc1.Left = 0; loc1.Top = 150; treeView1.Margin = loc1; TreeViewItem item = new TreeViewItem(); item.MouseLeftButtonUp += new MouseButtonEventHandler(item_MouseLeftButtonUp); item.Header = "Root"; SolidColorBrush border = new SolidColorBrush(); border.Color = SystemColors.HotTrackColor; treeView1.BorderBrush = border; treeView1.Items.Add(item); grdRoot.Children.Add(treeView1); } void item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (NodeClick != null) NodeClick(sender, new EventArgs()); } public string ButtonText { get { foreach (UIElement item in grdRoot.Children) { if (item.GetType().IsAssignableFrom(typeof(Button))) { return (item as Button).Content.ToString(); } } return string.Empty; } set { foreach (UIElement item in grdRoot.Children) { if (item.GetType().IsAssignableFrom(typeof(Button))) { (item as Button).Content=value; } } } } void treeView1_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Released) { if (NodeClick != null) NodeClick(sender, new EventArgs()); } } void btnItem_Click(object sender, RoutedEventArgs e) { if (ButtonClick != null) ButtonClick(sender, new EventArgs()); } } }
相关文章推荐
- CAD控件:易语言使用控件二次开发入门
- WPF控件开发(2) 自动完成(AutoComplete)-1
- WPF开发之限制输入的控件
- (转载)VS2010/MFC编程入门之五十四(Ribbon界面开发:使用更多控件并为控件添加消息处理函数)
- Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全
- WPF控件开发之自定义控件(2)
- Silverlight与WPF开发人员入门应该知道的十件事
- VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)
- WPF & ArcGIS Engine三维开发入门攻略(转)
- AspNet控件开发(1)---入门介绍
- WPF & ArcGIS Engine三维开发入门攻略
- 自定控件开发快速入门
- 8天入门wpf—— 第六天 细说控件
- iPhone 开发入门——程序画面与控件调整
- Kinect for Windows SDK开发入门(十八):Kinect Interaction交互控件
- WPF控件开发之控件概述(3)
- WPF一步步开发XMPP IM客户端1:入门
- 【翻译】WPF应用程序模块化开发快速入门(使用Prism+MEF)【下】
- wpf 控件开发基础(6) -单一容器(Decorator)
- VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)[转]