简体   繁体   English

选择使用MVVM更改事件

[英]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.

相关问题 如何使用MVVM将命令附加到WPF中的事件 - 更改DataGrid选择以更新TextBlock中显示的RowCount - How to Attach Command to Event in WPF Using MVVM - DataGrid Selection Changed to Update the RowCount Displayed in a TextBlock 如何在wp8中使用mvvm模式在listpicker中实现选择更改事件? - how to implement selection changed event in listpicker using mvvm pattern in wp8? 使用行为取消组合框选择更改事件 - Cancel Combobox Selection Changed event using behaviour 使用 MVVM 在 wpf 中处理标签的内容更改事件 - Handling Label's Content changed event in wpf using MVVM 在MVVM中绑定ListPicker(Selection_Changed) - Bind ListPicker in MVVM (Selection_Changed) 用户在C#中使用MVVM选择新项目时,只有射击选择发生更改 - Only fire selection changed when user selects new item using MVVM in C# 使用选定值(C#WPF)时,组合框选择已更改事件在选择更改之前触发 - Combo Box Selection Changed Event fires before Selection Changes when using Selected Value (C# WPF) 字段更改时MVVM对象触发属性更改事件 - MVVM object fire property changed event when a field is changed 更改字段时,MPF MVVM 对象不会触发属性更改事件 - MPF MVVM object not fire property changed event when a field is changed WebBrowser文本选择更改时的触发事件 - Trigger event when WebBrowser text selection changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM