繁体   English   中英

将项目添加到ListView的特定列

[英]Adding a Item to a ListView to a specific Column

我确实需要这样: 单击我

但是仅用于WPF。

而且我知道,我应该使用Binding等,但这只是为了进行自己的测试。

我还没有找到实现该目标的方法。

那这个呢:
是的,我知道这种尝试使用了具体的对象Person和binding,但是使用这种方法,只需修改所需的列/属性而无需照顾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>

代码隐藏:

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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM