WPF 自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分)
2024-09-01 19:09:51
region - 用于绑定ViewModel部分 -
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(MyUserControl), new PropertyMetadata(default(ICommand)));
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(MyUserControl), new PropertyMetadata(default(object)));
public IInputElement CommandTarget { get; set; }
#endregion
region 用于Xaml触发路由事件部分
//声明和注册路由事件
public static readonly RoutedEvent MyEventRoutedEvent =
EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(MyUserControl));
//CLR事件包装
public event RoutedEventHandler MyEvent
{
add { this.AddHandler(MyEventRoutedEvent, value); }
remove { this.RemoveHandler(MyEventRoutedEvent, value); }
}
//激发路由事件,借用Click事件的激发方法
protected void OnMyEvent()
{
RoutedEventArgs args = new RoutedEventArgs(MyEventRoutedEvent, this);
this.RaiseEvent(args);
}
#endregion
///
private void Button_Click(object sender, RoutedEventArgs e)
{
//命令作用于命令目标
if (this.Command != null)
{
this.Command.Execute(CommandParameter);
this.OnMyEvent();
}
}
<local:MyUserControl Grid.Row="1" Command="{Binding RelayCommand}" CommandParameter="Sumit">
local:MyUserControl.Triggers
</local:MyUserControl.Triggers>
</local:MyUserControl>
最新文章
- 【linux】scp命令
- C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法
- mysql分表和表分区详解
- LeetCode —— Invert Binary Tree
- Geolocation
- bxSlider 在网页里添加幻灯片效果
- Java Collections的排序之二
- 生日蛋糕 (codevs 1710) 题解
- 修改ptrace_scope
- iOS 使用FMDB SQLCipher给数据库加密
- mkimage的-a 和 –c参数和内核引导
- [Design Pattern] Iterator Pattern 简单案例
- chroot jail
- [Spark內核] 第41课:Checkpoint彻底解密:Checkpoint的运行原理和源码实现彻底详解
- USACO 2017 January Platinum
- IntelliJ IDEA 常用插件
- 【CentOS7.0】虚拟机如何实现扩展存储空间
- typescript 与 js 开发 react 的区别
- 数据库SQL语句性能优化
- python 获取有关访问者的浏览器的 细节
热门文章
- markdown中如何设置字体为红色?
- Angular中的routerLink 跳转页面和默认路由
- ISO/IEC 9899:2011 条款6.4.8——预处理数字
- Qt编写自定义控件56-波浪曲线
- 学习 TTreeView [16] - 给 TTreeView 添加复选框 (回复 ";丁永其"; 的问题)
- node.js使用superagent实现模拟登陆功能(包含下载验证码功能)
- 【Leetcode_easy】661. Image Smoother
- 【leetcode_easy】598. Range Addition II
- Direct2D 学习笔记 前言
- 最新 蚂蚁金服java校招面经 (含整理过的面试题大全)