【问题标题】:Simple ListView data binding简单的 ListView 数据绑定
【发布时间】:2013-06-04 22:26:38
【问题描述】:

我正在尝试使用 WPF 和 C# 在 ListView 中显示数据,但我对所看到的不同示例和方法感到困惑。我正在寻找一个与我的程序类似的完整工作示例,或使其工作的先决条件列表。如果我设法从我的集合中只显示 1 行数据,我会很高兴。目前,列表视图不显示任何内容。

C#:

public partial class MainWindow : Window
{
    public ObservableCollection<Row> Rows { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Rows = new ObservableCollection<Row>();
        Rows.Add(new Row 
        {
            ID = "42",
            Category = "cat",
            CharLimit = 32,
            Text = "Bonjour"
        });
    }
}

public class Row
{
    public string ID { get; set; }
    public string Category { get; set; }
    public int CharLimit { get; set; }
    public string Text { get; set; }
}

XAML:

<ListView ItemsSource="{Binding Path=Rows}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="200" Header="ID" DisplayMemberBinding="{Binding Path=ID}" />
            <GridViewColumn Width="200" Header="Category" DisplayMemberBinding="{Binding Path=Category}" />
            <GridViewColumn Width="200" Header="Text" DisplayMemberBinding="{Binding Path=Text}" />
        </GridView>
    </ListView.View>
</ListView>

提前致谢

【问题讨论】:

  • 看起来您没有在任何地方设置 DataContext,在这种情况下应该是 MainWindow 的实例。我还建议研究 MVVM 设计模式和 MVVM 框架。
  • 谢谢,我会查看一些 MVVM 教程来掌握这个想法并尝试正确地做 :)

标签: c# wpf listview data-binding binding


【解决方案1】:

您必须指定源,否则,就像在您的情况下,它将在当前上下文中查找属性,默认情况下,如果没有指定其他内容,它将是DataContext。试试这样的:

<ListView ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Rows}">

就像您指定它应该在当前的Window 中查找Rows

【讨论】:

  • 谢谢,在 C# 代码中添加 DataContext 很容易,并且可以正常工作。我还将研究@Arushi-Agrawal 的解决方案,因为当我更改行的属性时,我的 ListView 不会更新。
【解决方案2】:

创建一个viewmodel,可以将其设置为 XAML 的数据上下文

 public class WindowsViewModel
 {
    private ObservableCollection<RowViewModel> m_Rows;

    public ObservableCollection<RowViewModel> Rows
    {
        get { return m_Rows; }
        set { m_Rows = value; }
    }

    public WindowsViewModel()
    {
        Rows = new ObservableCollection<RowViewModel>();
        Rows.Add(new RowViewModel
            {
                ID = "42",
                Category = "cat",
                CharLimit = 32,
                Text = "Bonjour"
            });
    }
}

以下列方式实现类RowViewModel:

 public class RowViewModel:INotifyPropertyChanged
    {
      public RowViewModel()
      {
      }
      private string m_ID;
      public string ID
      { 
            get
            {
                return m_ID;
            } 
            set 
            {
                m_ID = value;
                NotifyPropertyChanged("ID");
            }
      }
      public string Category
      { 
            get; 
            set; 
      }

      public int CharLimit
      { 
        get; 
        set; 
      }

      public string Text 
      { 
        get;
        set;
      }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string Obj)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this,new PropertyChangedEventArgs(Obj));
        }
    }
}

在XAML后面的代码中,添加代码:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new WindowsViewModel();
        }
    }

在listview节点中添加更新源触发器属性:

<ListView ItemsSource="{Binding Rows, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="200" Header="ID" DisplayMemberBinding="{Binding Path=ID}" />
                    <GridViewColumn Width="200" Header="Category" DisplayMemberBinding="{Binding Path=Category}" />
                    <GridViewColumn Width="200" Header="Text" DisplayMemberBinding="{Binding Path=Text}" />
                </GridView>
            </ListView.View>

【讨论】:

  • 哇,感谢您的冗长回答。我意识到我对这个主题缺乏很多知识,所以我会在复制/粘贴您的答案之前阅读this MVVM tutorial。
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多