C#-WPF MVVM框架 图片或Button传递的命令参数为本身的内容Content或名字Name,并转换为枚举类型

逻辑为:View中的控件所含的字段或名字---> ViewModel中作为参数--->转换为枚举类型Enum

1、传递button的Content

View中代码:

<Button x:Name="壁装" Content="壁装" 
Command="{Binding QueryLampInstallingTypeCommand}" 
CommandParameter="{Binding Content,ElementName=壁装}">
</Button>

ViewModel中代码

 //构造函数
public RectangularLayoutViewModel()
        {
            QueryLampInstallingTypeCommand = new RelayCommand<object>(t => QueryLampInstallingType(t));
            
        }

//命令---通过单击事件获取枚举类型
public RelayCommand<object> QueryLampInstallingTypeCommand { get; set; }
//业务逻辑
private void QueryLampInstallingType(object t)
        {
            //将button或者触发事件中的CommandParameter绑定为Content或Name,传入字符串,进行字符串和枚举间的转型
            this._lampInstallingType = (LampInstallingType)Enum.Parse(typeof(LampInstallingType), t.ToString());
        }

2、传递Image的Name

        后台代码与1中一样

        View中代码:采用单击事件

<Image x:Name="壁装" Grid.Row="0" Grid.Column="1" Source="F:/EasyBIM/EleDesign/Ele_WXF/Images/WallMountedLamp.jpg" Margin="5">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <i:InvokeCommandAction  Command="{Binding QueryLampInstallingTypeCommand}"
                                                  CommandParameter="{Binding Name,ElementName=壁装}"              
                                        />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用MVVM架构的WPF应用程序中,可以使用以下方法将多个参数传递给CommandParameter: 1. 使用Tuples:使用C# 7.0及更高版本的元组功能可以将多个参数组合成一个元组并将其作为CommandParameter传递。例如: ``` <Button Content="Submit" Command="{Binding SubmitCommand}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding SubmitCommand}" CommandParameter="(1, 'Hello', true)" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> ``` 在ViewModel中,您可以使用元组解包这些参数: ``` public ICommand SubmitCommand => new RelayCommand<(int, string, bool)>(ExecuteSubmit); private void ExecuteSubmit((int, string, bool) args) { int arg1 = args.Item1; string arg2 = args.Item2; bool arg3 = args.Item3; // Do something with the parameters } ``` 2. 自定义类:创建一个包含多个属性的自定义类,然后将该类的实例作为CommandParameter传递。例如: ``` public class SubmitCommandParameter { public int Id { get; set; } public string Message { get; set; } public bool IsEnabled { get; set; } } <Button Content="Submit" Command="{Binding SubmitCommand}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding SubmitCommand}" CommandParameter="{StaticResource SubmitCommandParameter}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> <Window.Resources> <local:SubmitCommandParameter x:Key="SubmitCommandParameter" Id="1" Message="Hello" IsEnabled="true" /> </Window.Resources> public ICommand SubmitCommand => new RelayCommand<SubmitCommandParameter>(ExecuteSubmit); private void ExecuteSubmit(SubmitCommandParameter parameter) { int id = parameter.Id; string message = parameter.Message; bool isEnabled = parameter.IsEnabled; // Do something with the parameters } ``` 希望这些示例能够帮助您在MVVMWPF应用程序中使用InvokeCommandAction传递多个参数作为CommandParameter。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值