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

相关推荐
xiaoshuaishuai819 小时前
C# AvaloniaUI ProgressBar用法
开发语言·c#
闪电悠米20 小时前
黑马点评-秒杀优化-02_lua_precheck
开发语言·redis·分布式·缓存·junit·wpf·lua
wearegogog1231 天前
基于C#的电机监控上位机(串口通信+实时波形)
开发语言·c#
△曉風殘月〆1 天前
C#如何Hook托管函数
c#·hook
FuckPatience1 天前
WPF 获取一个控件某个依赖属性的默认绑定方式
wpf
雪豹阿伟1 天前
18.C# —— 三层结构 + 接口架构实战(UI+Model+DAL+IDAL)
c#·上位机
雪豹阿伟1 天前
17.C# —— 事件
c#·上位机
weixin_428005301 天前
C#调用 AI学习从0开始-第2阶段(Function Calling+工具调用智能体)-第9天实战
人工智能·学习·ai·c#·functioncalling
FuckPatience1 天前
C# 继承中的使用new的陷阱,和abstract /virtual 的不同
开发语言·c#
z落落1 天前
C# 索引器 this[]
开发语言·c#