【问题标题】:Button inside a WPF List view/data gridWPF 列表视图/数据网格内的按钮
【发布时间】:2011-12-14 11:47:25
【问题描述】:

我正在尝试获取单击行的值/ID。如果选择了该行,则可以正常工作。但是如果我只是尝试单击里面的按钮,则所选客户为空。我如何在这里做命令参数。
我试过这个,看看以下问题的答案:

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 方法中,选定的 Customer 以 null 的形式出现。我想要这里的数据行(客户)。我该怎么做?

【问题讨论】:

  • 那么如果选择了该行然后你点击了它的工作按钮?您是否尝试过自动选择单击按钮所在的行?
  • 你这是什么意思?你如何自动选择行?用户选择它。我很困惑

标签: wpf mvvm mvvm-light wpfdatagrid


【解决方案1】:

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

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

&lt;Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter={Binding} /&gt;

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

并且需要修改 OnRun 方法以获取参数作为参数。

私人无效OnRun(对象o){ if(!(o 是客户)) 返回; // 做一点事 }
  • 或者,使用 SelectionChanged 事件处理编写一些代码隐藏。 (不推荐)

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

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    相关资源
    最近更新 更多