gpt4 book ai didi

WPF - 带有按钮的 ListView

转载 作者:行者123 更新时间:2023-12-04 05:34:27 24 4
gpt4 key购买 nike

我有一个 ListView 模板,其中一列是一个按钮。单击此按钮时,我需要选定的项目。我怎么能这样做?

最佳答案

要在按钮按下事件中捕获选定的 ListView 项,您可以利用 MVVM 模式。在我的 ListView 中,在 XAML 中,我将 ItemsSource 和 SelectedItem 绑定(bind)到 ViewModel 类。我还将模板中的按钮 Command 绑定(bind)到 ViewModel 中的 RunCommand。

棘手的部分是让从模板到事件 DataContext 的绑定(bind)正确。

完成此操作后,您可以在 RunCommand 中捕获 SelectedCustomer
按下按钮时执行。

我已经包含了一些代码来帮助您入门。
您可以通过 Google 找到 ViewModelBase 和 DelegateCommand 的实现。

这是 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>

这是 View 模型:
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;
}
}
}

这是我在 ViewModel 中链接到 View 的地方:
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
DataContext = new ViewModels.MainViewModel();
}
}

关于WPF - 带有按钮的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4921046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com