当前位置:   article > 正文

在WPF程序中如何给自定义控件添加属性及方法_wpf prism 访问自定义控件的属性

wpf prism 访问自定义控件的属性

添加属性

在控件.cs文件中添加依赖属性(快捷键propdp Tab键)

  1. public string Text
  2. {
  3. get { return (string)GetValue(TextProperty); }
  4. set { SetValue(TextProperty, value); }
  5. }
  6. public static readonly DependencyProperty TextProperty =
  7. DependencyProperty.Register("Text", typeof(string),
  8. typeof(Control1), new PropertyMetadata(""));

在界面中需要显示的位置绑定依赖属性

  1. <TextBlock Text="{Binding RelativeSource=
  2. {RelativeSource FindAncestor,AncestorType={x:Type UserControl}},
  3. Path=Text}"/>

 这样使用控件的人就可以在自定义控件中对添加的Text属性进行访问,编辑和绑定

添加方法

根据所需的控件名称及事件名称更改

  1. public event RoutedEventHandler Execute
  2. {
  3. add {this.AddHandler(ExecuteEven, value); }
  4. remove { this.RemoveHandler(ExecuteEven, value); }
  5. }
  6. public static readonly RoutedEvent ExecuteEven =
  7. EventManager.RegisterRoutedEvent("Execute", RoutingStrategy.Bubble,
  8. typeof(RoutedEventHandler), typeof(BottomControl));

在想要调用的事件中添加

  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. RoutedEventArgs arg = new RoutedEventArgs(ExecuteEven, this);
  4. RaiseEvent(arg);
  5. }

添加之后,当在点击自定义控件中的Button按钮时会触发ExecuteEven事件,使用自定义控件时可以添加ExecuteEven事件进行处理

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/746044
推荐阅读
相关标签
  

闽ICP备14008679号