WPF - 列表视图与按钮 [英] WPF - Listview with button

查看:84
本文介绍了WPF - 列表视图与按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图模板和一列是一个按钮。我需要选中的项目,当我在这个按钮点击。我怎样才能做到这一点?

i have a listview template and one column is a button. I need selected item when i click in this button. How i can do this ??

推荐答案

要cature您可以利用MVVM模式按钮pressed事件中选定的ListView项。在我的ListView,在XAML,我绑定的ItemsSource和selectedItem设置一个ViewModel类。我也有我的按钮命令绑定在模板中的视图模型来RunCommand。

To cature the selected ListView item inside a button pressed event you can leverage the MVVM pattern. In my ListView, in the XAML, I bind the ItemsSource and SelectedItem to a ViewModel class. I also bind my button Command in the template to RunCommand in the ViewModel.

棘手的部分是获得正确的结合从模板​​到活动的DataContext。

The tricky part is getting the binding correct from the template to the active DataContext.

一旦你这样做,你可以捕捉到RunCommand内SelectedCustomer说
当按钮获得pressed得到执行。

Once you do this you can capture the SelectedCustomer inside the RunCommand that gets executed when the button gets pressed.

我已经包含了一些code,以帮助您开始。
您可以通过谷歌找到ViewModelBase和DelegateCommand的实现。

I've included some of the code to help get you started. You can find implementations of ViewModelBase and DelegateCommand via Google.

下面是XAML:

<Window x:Class="ListViewScrollPosition.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="400">
  <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 FirstName}"/>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Last Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Margin="6,2,6,2">
                                <TextBlock Text="{Binding LastName}"/>
                            </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>
</Window>

下面是视图模型:

using System.Collections.ObjectModel;
using System.Windows.Input;
using ListViewScrollPosition.Commands;
using ListViewScrollPosition.Models;

namespace ListViewScrollPosition.ViewModels
{
  public class MainViewModel : ViewModelBase
  {
    public ICommand RunCommand { get; private set; }

    public MainViewModel()
    {
      RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand);
      _customers = Customer.GetSampleCustomerList();
      _selectedCustomer = _customers[0];
    }

    private ObservableCollection<Customer> _customers = 
                    new ObservableCollection<Customer>();
    public ObservableCollection<Customer> Customers
    {
      get
      {
        return _customers;
      }
    }

    private Customer _selectedCustomer;
    public Customer SelectedCustomer
    {
      get
      {
        return _selectedCustomer;
      }
      set
      {
        _selectedCustomer = value;
        OnPropertyChanged("SelectedCustomer");
      }
    }

    private void OnRunCommand(object obj)
    {
      // use the SelectedCustomer object here...
    }

    private bool CanRunCommand(object obj)
    {
      return true;
    }
  }
}

下面是我的视图模型链接查看:

Here is where I link in the ViewModel to the View:

public partial class MainView : Window
{
  public MainView()
  {
     InitializeComponent();
     DataContext = new ViewModels.MainViewModel();
  }
}

这篇关于WPF - 列表视图与按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆