将项目添加到ListView到特定列

时间:2012-01-24 14:27:24

标签: c# wpf listview

我需要这个:Click Me

但仅限WPF。

我知道,我应该使用Binding等,但它仅用于自己的测试。

我还没有发现我是如何实现这一目标的。

2 个答案:

答案 0 :(得分:3)

这个怎么样:
是的我知道这个尝试使用了一个具体的对象 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"));
        }
      }
    }

  }

答案 1 :(得分:2)

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