WPF Listbox selectionchanged MVVM
我的WPF应用程序中有一个列表框。 我知道如何使用selectionchanged事件。 但是我试图遵循MVVM设计。 但是我不知道该怎么做。
我已经为一个按钮做了这个,但不确定我是否可以做同样的事情?
public class ViewModel : INotifyPropertyChanged { // for the button that imports the orders file public ICommand CommandButtImport { get; set; } public ViewModel() { CommandButtImport = new MyCommands( ExecuteCommandButtImport, CanExecuteCommandButtImport); } private bool CanExecuteCommandButtImport(object parameter) { return true; } // import button private void ExecuteCommandButtImport(object parameter) { // some code } }
编辑请忽略上面的代码
我已经更改了我的代码,所以在我目前拥有的内容之下重新发布。 我有一个奇怪的问题。 XAML – 主代码包含我的数据网格的代码。 App – XAML下面的块包含我的大多数应用程序的样式,但只是一个snipet。
也
代码行添加到我的数据网格下面的XAML – 主代码中用于测试目的。
我的datagrid加载很好。 当我点击一行时,该行会展开以显示证券列表。 我遇到的问题是,当我单击某个项目时,在此列表框中没有任何反应。 但是,我在数据网格下面添加的Listbox用于测试目的确实有效。 例如,我单击其中一个项目并更新我的行,同时我的行详细信息中的列表框也会被选中。 很奇怪为什么我的rowdetail中的列表框不起作用,但是我的数据网格下面的列表框不起作用。 有任何想法吗?
XAML – 主要代码
App XAML
视图模型
public class ViewModel : INotifyPropertyChanged { public ICommand CommandButtImport { get; set; } // for the button that imports the orders file public ICommand CommandButtSend { get; set; } // the button where the user sends the orders in our data grid to thinkFolio public ICommand CommandButtExit { get; set; } // exit application private QoEMain _QoEManager; // manages the Model public QoEMain QoEManager { get { return this._QoEManager; } set { _QoEManager = value; } } private OrderBlocks _orderBlock; // order block - contains all the order information public OrderBlocks OrderBlock { get { return this._orderBlock; } set { this._orderBlock = value; OnPropertyChanged("OrderBlock"); } } }
OrderBlocks类,包含其他类
public class OrderBlocks : INotifyPropertyChanged { private List _orders; [XmlElement("tF_Transactions")] public List Orders { get { return _orders; } set { _orders = value; OnPropertyChanged("Orders"); } } }
订单类
public class Order : INotifyPropertyChanged { Security security; public Security Security { get { return security; } set { security = value; OnPropertyChanged("Security"); } } List duplicateSecurities; public List DuplicateSecurities { get { return duplicateSecurities; } set { duplicateSecurities = value; OnPropertyChanged("DuplicateSecurities"); } }
安全等级
public class Security : INotifyPropertyChanged { private string _id; public string ID { get { return _id; } set { _id = value; OnPropertyChanged("ID"); } } private string _name; public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } public Security() { } public Security(string newID, string newName) { ID = newID; Name = newName; }
编辑 – 我的代码现在可以使用,请参阅下面适用于我的代码段
<DataGrid Grid.Row="1" Grid.Column="0" ItemsSource="{Binding SelectedItem.DuplicateSecurities, ElementName=dataGridOrders}" SelectedItem="{Binding SelectedItem.Security, ElementName=dataGridOrders}"
将ListBox SelectionChanged
事件绑定到ViewModel中的命令示例
在您的ViewModel
:
public class myViewModel { public myViewModel() { SelectedItemChangedCommand = new DelegateCommand
此特定示例使用Prism MVVM Framework,但您可以将相同的想法应用于您正在使用的任何其他MVVM框架。
希望这可以帮助
使用SelectionChanged
和MVVM非常简单:
一种方法可能是绑定到ListBox
的SelectedIndex
属性并在VM中的属性设置器中,相应地执行操作,因为只要属性更改它就会被触发。
示例: 这里
在此示例中,只要所选索引发生更改,该项的值就会增加1。
主要是:
public int SelectedIndex { get { return _selectedIndex; } set { if (_selectedIndex == value) { return; } // At this point _selectedIndex is the old selected item's index _selectedIndex = value; // At this point _selectedIndex is the new selected item's index RaisePropertyChanged(() => SelectedIndex); } }
xaml只是:
Items
是我们绑定的项目的集合。