逻辑为: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>