Button inside a WPF List view/data grid

2019-04-11 21:13发布

问题:

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?

回答1:

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)



回答2:

CommandParameter does NOT entirely support data binding.

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