我想通过使用RelayCommand将我的应用程序的XAML(View)中定义的参数传递给ViewModel类.我关注了Josh Smith关于MVVM的优秀文章,并实现了以下内容.
XAML代码
<Button
Command="{Binding Path=ACommandWithAParameter}"
CommandParameter="Orange"
HorizontalAlignment="Left"
Style="{DynamicResource SimpleButton}"
VerticalAlignment="Top"
Content="Button"/>
Run Code Online (Sandbox Code Playgroud)
ViewModel代码
public RelayCommand _aCommandWithAParameter;
/// <summary>
/// Returns a command with a parameter
/// </summary>
public RelayCommand ACommandWithAParameter
{
get
{
if (_aCommandWithAParameter == null)
{
_aCommandWithAParameter = new RelayCommand(
param => this.CommandWithAParameter("Apple")
);
}
return _aCommandWithAParameter;
}
}
public void CommandWithAParameter(String aParameter)
{
String theParameter = aParameter;
}
#endregion
Run Code Online (Sandbox Code Playgroud)
我在CommandWithAParameter方法中设置了一个断点,并观察到aParameter设置为"Apple",而不是"Orange".这看起来很明显,因为使用文字字符串"Apple"调用CommandWithAParameter方法.
但是,查看执行堆栈,我可以看到"Orange",我在XAML中设置的CommandParameter是RelayCommand实现ICommand Execute接口方法的参数值.
也就是执行堆栈下面方法中参数的值是"Orange",
public void Execute(object parameter)
{
_execute(parameter);
}
Run Code Online (Sandbox Code Playgroud)
我想弄清楚的是如何创建RelayCommand ACommandWithAParameter属性,以便它可以使用XAML中定义的CommandParameter"Orange"调用CommandWithAParameter方法.
有没有办法做到这一点?
我为什么要这样做?"On The …
有一个名为Student具有属性的类Id,Name和Phone.在UI表单中,有一个Student以下列方式的列表:
List<Student> students=new List<Student>();
Run Code Online (Sandbox Code Playgroud)
最后有一个dataGridview_Cellclick事件代码,使用下面的代码:
string id = dataGridview.Rows[e.RownIndex].Cells[0].Value.ToString();
Student aStudent = students.Find(i=> i.Id== id);
Run Code Online (Sandbox Code Playgroud)
怎么students.Find(i=> i.Id== id)办?这是什么意思?=>标志是什么意思?它是如何工作的?