i have a listview template and one column is a button. I need selected item when i click in this button. How i can do this ??
相关问题
- VNC control for WPF application
- How to create Circular view on android wear?
- WPF Binding from System.Windows.SystemParameters.P
- JSON Exception - No value for wanted parameter
- Android: ListView rounded corner hidden by list it
To cature the selected ListView item inside a button pressed event you can leverage the MVVM pattern. In my ListView, in the XAML, I bind the ItemsSource and SelectedItem to a ViewModel class. I also bind my button Command in the template to RunCommand in the ViewModel.
The tricky part is getting the binding correct from the template to the active DataContext.
Once you do this you can capture the SelectedCustomer inside the RunCommand that gets executed when the button gets pressed.
I've included some of the code to help get you started. You can find implementations of ViewModelBase and DelegateCommand via Google.
Here is the XAML:
Here is the ViewModel:
Here is where I link in the ViewModel to the View:
Example with a regular click event in the code behind:
Code behind:
Note: While I certainly appreciate MVVM, I've come to accept that there is a pretty steep slope of dimminishing returns once you cross into actions and messaging between the form and the VM, so I use it only in cases of complex relationships between VMs or large singular VMs. For CRUD style data-centric applications I prefer to handle actions and message relay with the code behind.