[英]Selection Changed Event using MVVM
I have a listbox that contains a list of persons. 我有一个包含人员列表的列表框 。 When the user clicks on an item the viewModel should set the currentPerson object to the Object the user has clicked on.
当用户单击某个项时,viewModel应将currentPerson对象设置为用户单击的Object。
I have to use a ViewModel for this, so no code inside the MainWindow.xaml.xs . 我必须为此使用ViewModel,因此MainWindow.xaml.xs中没有代码 。 Any Ideas how to solve this?
任何想法如何解决这个问题?
That's very simple: 这很简单:
Add a property CurrentPerson
to your ViewModel and bind it to the SelectedItem
property of the ListBox. 将属性
CurrentPerson
添加到ViewModel并将其绑定到ListBox的SelectedItem
属性。
Something like this: 像这样的东西:
View Model: 查看型号:
public Person CurrentPerson
{
get { return _currentPerson; }
set
{
if(value == _currentPerson) return;
_currentPerson = value;
NotifyOfPropertyChange("CurrentPerson");
}
}
View: 视图:
<ListBox SelectedItem="{Binding CurrentPerson}" ...>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.