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

发布于 2024-12-12 05:54:33 字数 2807 浏览 0 评论 0原文

我正在尝试获取单击的行的值/ID。如果选择了该行,则效果很好。但如果我只是尝试单击里面的按钮,则所选客户为空。我如何在这里设置命令参数。
我尝试查看以下问题的答案:

ListView 和 ListView 内的按钮

WPF - 带按钮的列表视图

这是代码:

<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 方法中,选择的客户是作为空值进来。我想要这里的数据行(客户)。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2024-12-19 05:54:33

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

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

我认为 CommandParameter={Binding} 工作正常,因为每行的 DataContext 都是每个 Customer 对象。

需要修改 OnRun 方法才能获取参数作为参数。

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

  • 或者,使用 SelectionChanged 事件处理编写一些隐藏代码。 (不推荐)

  • 或者,使用MVVM-light工具包中的EventToCommand。 (不推荐)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文