Note that there are some explanatory texts on larger screens.

plurals
  1. POButton inside a WPF List view/data grid
    primarykey
    data
    text
    <p>I am trying to get the value/ID of the clicked row. If the row is selected, this works fine. But if I just try to click the button inside, the selected customer is null. How do I do Command Parameters here.<br> I tried this see the answers to the following questions:</p> <p><a href="https://stackoverflow.com/questions/3045872/listview-and-buttons-inside-listview">ListView and Buttons inside ListView</a></p> <p><a href="https://stackoverflow.com/questions/4921046/wpf-listview-with-button">WPF - Listview with button</a></p> <p>Here is the code: </p> <pre><code>&lt;Grid&gt; &lt;ListView ItemsSource="{Binding Path=Customers}" SelectedItem="{Binding Path=SelectedCustomer}" Width="Auto"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="First Name"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Margin="6,2,6,2"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Address"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Margin="6,2,6,2"&gt; &lt;Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; public class VM : ViewModelBase { public RelayCommand RunCommand { get; private set; } private ObservableCollection&lt;Customer&gt; _Customers; public ObservableCollection&lt;Customer&gt; Customers { get { return _Customers; } set { if (value != _Customers) { _Customers = value; RaisePropertyChanged("Customers"); } } } private Customer _SelectedCustomer; public Customer SelectedCustomer { get { return _SelectedCustomer; } set { if (value != _SelectedCustomer) { _SelectedCustomer = value; RaisePropertyChanged("SelectedCustomer"); } } } public VM() { Customers = Customer.GetCustomers(); RunCommand = new RelayCommand(OnRun); } private void OnRun() { Customer s = SelectedCustomer; } } </code></pre> <p>in the OnRun Method, selected Customer is coming in as null. I want the data row(customer) here. How do I do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload