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

相关推荐
两千次23 分钟前
图像的处理 图像转haclon
c#
bugcome_com4 小时前
C# 运算符详解:类型、实例及优先级
c#
C#程序员一枚6 小时前
Web Service 和 Web API
c#
sdff1139617 小时前
【HarmonyOS】鸿蒙Flutter跨设备流转技术实战指南
flutter·wpf·harmonyos
MaoziShan1 天前
CMU Subword Modeling | 09 Lexemes, or What Dictionaries Know about Morphology
开发语言·人工智能·机器学习·语言模型·自然语言处理·c#
游乐码1 天前
c#选择排序
c#·排序算法
listhi5201 天前
基于C#实现动态人脸检测
开发语言·c#
rabbitlzx1 天前
《Async in C# 5.0》第十四章 深入探讨编译器对于async的转换
java·开发语言·c#·异步·asynchronous
智商偏低1 天前
unity 如何渲染大场景
c#