WPF列表视图/数据网格中的按钮

3
我正在尝试获取所点击行的值/ID。如果选择了该行,则可以正常工作。但是,如果我只是尝试点击其中的按钮,则选择的客户将为null。如何在此处使用命令参数。
我尝试了以下代码,请参见以下问题的答案: ListView and Buttons inside ListView WPF - Listview with button 以下是代码:
<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;
    }
}

在OnRun方法中,选定的客户传入为null。我想在此处获取数据行(客户)。我该怎么做?

那么,如果这一行被选中并且您点击了按钮,它会起作用吗?您是否尝试过在单击按钮时自动选择该行? - d.moncada
你是什么意思?如何自动选择行?用户进行选择。我感到困惑。 - katie77
2个回答

9

您可以选择三种可能的解决方案。

  • 将当前行对象作为CommandParameter传递。在这种情况下,您需要稍微修改OnRun方法。(推荐)

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

我认为CommandParameter={Binding}的效果很好,因为每行的DataContext都是每个Customer对象。

同时需要修改OnRun方法以获取参数作为参数。

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

  • 或者,在代码后台编写SelectionChanged事件处理程序(不建议)。

  • 或者,在MVVM-light工具包中使用EventToCommand(不建议)。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接