WPF - Listview with Button

I have a list template, and one column is a button. I need the selected item when I click this button. How can i do this?

+8
listview button wpf
source share
2 answers

To pin the selected ListView element inside the button you clicked, you can use the MVVM template. In my ListView, in XAML, I bind ItemsSource and SelectedItem to the ViewModel class. I also bind my Command button in the template to RunCommand in the ViewModel.

The hard part gets the binding right from the template to the active DataContext.

Once you do this, you can capture the SelectedCustomer inside the RunCommand, which runs when the button is clicked.

I have included a piece of code to help you get started. You can find implementations of ViewModelBase and DelegateCommand through Google.

Here is the XAML:

<Window x:Class="ListViewScrollPosition.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Main Window" Height="400" Width="400"> <Grid> <ListView ItemsSource="{Binding Path=Customers}" SelectedItem="{Binding Path=SelectedCustomer}" Width="Auto"> <ListView.View> <GridView> <GridViewColumn Header="First Name"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Margin="6,2,6,2"> <TextBlock Text="{Binding FirstName}"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Last Name"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Margin="6,2,6,2"> <TextBlock Text="{Binding LastName}"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Address"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Margin="6,2,6,2"> <Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> </Grid> </Window> 

Here is the ViewModel model:

 using System.Collections.ObjectModel; using System.Windows.Input; using ListViewScrollPosition.Commands; using ListViewScrollPosition.Models; namespace ListViewScrollPosition.ViewModels { public class MainViewModel : ViewModelBase { public ICommand RunCommand { get; private set; } public MainViewModel() { RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand); _customers = Customer.GetSampleCustomerList(); _selectedCustomer = _customers[0]; } private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>(); public ObservableCollection<Customer> Customers { get { return _customers; } } private Customer _selectedCustomer; public Customer SelectedCustomer { get { return _selectedCustomer; } set { _selectedCustomer = value; OnPropertyChanged("SelectedCustomer"); } } private void OnRunCommand(object obj) { // use the SelectedCustomer object here... } private bool CanRunCommand(object obj) { return true; } } } 

Here I am linking in ViewModel with a view:

 public partial class MainView : Window { public MainView() { InitializeComponent(); DataContext = new ViewModels.MainViewModel(); } } 
+13
source share

An example with a regular click event in the code behind:

 <ListView Height="167.96" VerticalAlignment="Top" ItemsSource="{Binding FulfillmentSchedules}" SelectedItem="{Binding SelectedFulfillmentSchedule}"> <ListView.View> <GridView> <GridViewColumn Header="Request"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0}-{1}-{2}"> <Binding Path="Template.ProjectNumber" /> <Binding Path="Template.JobNumber" /> <Binding Path="Template.RequestId" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Template" DisplayMemberBinding="{Binding Template.Name}"/> <GridViewColumn Header="Start Date" DisplayMemberBinding="{Binding StartDate}"/> <GridViewColumn Header="Records" DisplayMemberBinding="{Binding Parameters.Records}"/> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <Button Name="BtnYourButton" Content="Your Button" Click="BtnYourButton_Click" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> 

Code behind:

 private void BtnYourButton_Click(object sender, RoutedEventArgs e) { var boundData= (YourBoundDataType)((Button)sender).DataContext; //do what you need to do here, including calling other methods on your VM } 

Note. Despite the fact that I certainly appreciate MVVM, I agreed that after you move on to actions and messaging between the form and the virtual machine, there is a rather steep slope of diminishing results, so I only use it in cases of complex The relationship between virtual machines or large singular virtual machines. For CRUD-style style applications, I prefer to handle actions and relay messages with code.

+3
source share

All Articles