WPF中CommandParameter用法

1. 界面样式

2. XAML中代码部分

cs 复制代码
<Button
    Grid.Row="0"
    Grid.Column="1"
    Command="{Binding BtnClick_Number}"
    CommandParameter="7"
    Content="7"
    Style="{StaticResource BtnStyle_Num}" />
<Button
    Grid.Row="0"
    Grid.Column="2"
    Command="{Binding BtnClick_Number}"
    CommandParameter="8"
    Content="8"
    Style="{StaticResource BtnStyle_Num}" />

3. ViewModel中代码部分

cs 复制代码
public RelayCommand<object?> BtnClick_Number { get; }

//构造函数中:
BtnClick_Number = new RelayCommand<object?>(OperNumber);

//函数实现:
private void OperNumber(object? obj)
{
    if (FxShow != null && obj != null)
    {
        FxShow = FxShow.Insert(FxShowSelectStart, obj.ToString()!);
        FxShowSelectStart++;
    }
}

这样便可以实现在一个函数(OperNumber)中根据不同的CommandParameter区分不同的按钮点击事件。

相关推荐
江沉晚呤时3 小时前
使用 C# 和 Semantic Kernel 构建 PDF 向量搜索系统:从文本提取到语义搜索
jvm·人工智能·microsoft·chatgpt·c#
豫狮恒3 小时前
OpenHarmony Flutter 分布式音视频:跨设备实时流传输与协同播放方案
分布式·flutter·wpf·openharmony
500843 小时前
鸿蒙 Flutter 安全组件开发:加密输入框与脱敏展示组件
flutter·华为·electron·wpf·开源鸿蒙
null_null9993 小时前
wpf 库的图片不显示
wpf
豫狮恒3 小时前
OpenHarmony Flutter 分布式任务调度:跨设备负载均衡与资源优化方案
分布式·flutter·wpf·openharmony
极客智造3 小时前
WPF 自定义可交互 Tester 控件:拖动、缩放、内容承载与层级管理全方案
wpf
唐青枫3 小时前
LINQ 新时代:CountBy、AggregateBy 深度解析(含对比 GroupBy)
c#·.net
豫狮恒4 小时前
OpenHarmony Flutter 原子化服务开发实战:轻量、跨端、分布式的全场景落地
flutter·wpf·openharmony
yue00816 小时前
C# winform自定义控件
开发语言·c#
_Cherry|18 小时前
Unity读取文件夹内容
unity·c#