赞
踩
在控件.cs文件中添加依赖属性(快捷键propdp Tab键)
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value); }
- }
-
- public static readonly DependencyProperty TextProperty =
- DependencyProperty.Register("Text", typeof(string),
- typeof(Control1), new PropertyMetadata(""));
在界面中需要显示的位置绑定依赖属性
- <TextBlock Text="{Binding RelativeSource=
- {RelativeSource FindAncestor,AncestorType={x:Type UserControl}},
- Path=Text}"/>
这样使用控件的人就可以在自定义控件中对添加的Text属性进行访问,编辑和绑定
根据所需的控件名称及事件名称更改
- public event RoutedEventHandler Execute
- {
- add {this.AddHandler(ExecuteEven, value); }
- remove { this.RemoveHandler(ExecuteEven, value); }
- }
- public static readonly RoutedEvent ExecuteEven =
- EventManager.RegisterRoutedEvent("Execute", RoutingStrategy.Bubble,
- typeof(RoutedEventHandler), typeof(BottomControl));
在想要调用的事件中添加
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- RoutedEventArgs arg = new RoutedEventArgs(ExecuteEven, this);
- RaiseEvent(arg);
- }
添加之后,当在点击自定义控件中的Button按钮时会触发ExecuteEven事件,使用自定义控件时可以添加ExecuteEven事件进行处理
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。