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区分不同的按钮点击事件。

相关推荐
宝桥南山16 小时前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=016 小时前
C# 中 Lambda、Linq 与 Expression
c#·linq
光泽雨18 小时前
WPF 音视频四大核心方案对比
wpf·音视频
zhanghaha131418 小时前
C# 系列教程_7 判断语句(分支)保姆级精讲
开发语言·c#
zhanghaha131418 小时前
C# 系列教程_8 循环语句 保姆级精讲(面向新手)
开发语言·c#
rockey6271 天前
AScript之LINQ扩展
c#·.net·script·动态脚本
1-1=01 天前
C# 委托与事件
开发语言·c#
浪扼飞舟1 天前
WPF(数据绑定)
wpf
1-1=02 天前
C# 特性(Attribute)
c#·.net·.netcore
samble2 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例