将项添加到特定列的ListView中

本文关键字:ListView 添加 | 更新日期: 2023-09-27 18:21:11

我需要这个:点击我

但只是为了WPF。

我知道,我应该使用绑定等,但这只是为了自己的测试。

我还不知道怎样才能做到这一点。。

将项添加到特定列的ListView中

这个怎么办:
是的,我知道这种尝试使用了一个具体的对象Person和绑定,但使用这种方法,只需修改所需的列/属性就非常简单,而不必考虑ListView本身。我认为带有绑定的方法比任何其他方法都简单。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ListView ItemsSource="{Binding}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"></GridViewColumn>
                    <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"></GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Window>

C#:

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      ObservableCollection<Person> list = new ObservableCollection<Person>();
      list.Add(new Person() { FirstName = "Firstname", LastName = "Lastname"});
      DataContext = list;
    }
  }
  public class Person : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged; 
    private string _firstName;
    public string FirstName {
      get
      {
        return _firstName;
      }
      set
      {
        _firstName = value;
        if (PropertyChanged != null)
        {
          PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
        }
      }
    }
    private string _lastName;
    public string LastName 
    {
      get
      {
        return _lastName;
      }
      set
      {
        _lastName = value;
        if (PropertyChanged != null)
        {
          PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
        }
      }
    }
  }

在WPF中,当更改项目时,您几乎总是在绑定数据中进行更改,并将这些更改通知UI。因此,您必须使用ObservableCollection并实现INotifyPropertyChanged

请不要像在WinForms中那样试图解决WPF问题,因为架构和编程风格完全不同,你会在某个时候陷入困境。。。

XAML:

  <Grid>
    <StackPanel>
      <ListView x:Name="MyListView">
        <ListView.View>
          <GridView>
            <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Width="140" Header="City" DisplayMemberBinding="{Binding City}"/>
          </GridView>
        </ListView.View>
      </ListView>
      <Button Height="50" Click="Button_Click">Click</Button>
    </StackPanel>
  </Grid>

CodeBehind:

private ObservableCollection<MyData> _data;
public MainWindow()
{
  InitializeComponent();
   _data = new ObservableCollection<MyData>()
                                        {
                                          new MyData() {City = "City1", Name = "Name1"},
                                          new MyData() {City = "City2", Name = "Name2"},
                                          new MyData() {City = "City3", Name = "Name3"},
                                        };
  MyListView.ItemsSource = _data;
}
public class MyData : INotifyPropertyChanged
{
  private string _name;
  public string Name
  {
    get { return _name; }
    set { 
      _name = value;
      OnPropertyChanged("Name");
    }
  }
  private string _city;
  public string City
  {
    get { return _city; }
    set
    {
      _city = value;
      OnPropertyChanged("City");
    }
  }
  public event PropertyChangedEventHandler PropertyChanged;
  public void OnPropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
  _data[1].City = "NewValue";
}