3

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.
I tried this see the answers to the following questions:

ListView and Buttons inside ListView

WPF - Listview with button

Here is the code:

<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 Name}"/>
                            </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>

public class VM : ViewModelBase
{
    public RelayCommand RunCommand { get; private set; }
    private ObservableCollection<Customer> _Customers;
    public ObservableCollection<Customer> 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;
    }
}

in the OnRun Method, selected Customer is coming in as null. I want the data row(customer) here. How do I do this?

2
  • So if the row is selected and then you click the button it works? Have you tried automatically selecting the row the button is clicked from? Commented Oct 24, 2011 at 23:26
  • What do you mean by that? How do you automatically select the row? The user selects it. I am confused Commented Oct 25, 2011 at 0:35

2 Answers 2

10

Three possible solutions that you can choose.

  • Pass current row object as a CommandParameter. In this case, you will modify OnRun method a little. (recommended)

<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter={Binding} />

I think CommandParameter={Binding} works fine because the DataContext of each row is each Customer object.

and OnRun method needs to be modified in order to get a parameter as an argument.

    private void OnRun(object o){
        if(!(o is Customer)) return;
        // Do something
    }

  • Or, Write a little code-behind with SelectionChanged event handling. (not recommended)

  • Or, Use EventToCommand in MVVM-light toolkit. (not recommended)

Sign up to request clarification or add additional context in comments.

Comments

0

CommandParameter does NOT entirely support data binding.

http://connect.microsoft.com/VisualStudio/feedback/details/472932/wpf-commandparameter-binding-should-be-evaluated-before-command-binding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.