【问题标题】:Binding DataTable using ObservableCollection to ListView使用 ObservableCollection 将 DataTable 绑定到 ListView
【发布时间】:2015-09-02 14:12:58
【问题描述】:

我看到这是一个常见问题,因为我浏览了很多帖子,但无法为我的帖子找到解决方案。我正在从WinForms 迁移到WPF,并且有一个需要绑定到ListViewDataSet。我试图遵循这个解决方案:https://stackoverflow.com/a/5880315/2920121 但没有成功。 请注意,我是 C# 和 .NET 的新手。

这是我的 WPF 和 WinForms 代码:

WinForms:

CmdStrFg = "SELECT dbname, Version, UpdateTime FROM(...)as tbl2 ";

CmdStrBlm = "SELECT dbname, Version, UpdateTime FROM (...)as tbl1 ";

CmdStrPay = "SELECT dbname, Version, UpdateTime FROM(...)as tbl3 ";

CmdStrTran = "SELECT dbname, Version, UpdateTime FROM(...)as tbl4";

DataSet ds = new DataSet();

ds.Tables.Add(GetDataFromDatabase(GetConnectionString(conStringBlm), CmdStrBlm));
ds.Tables.Add(GetDataFromDatabase(GetConnectionString(conStringFg), CmdStrFg));
ds.Tables.Add(GetDataFromDatabase(GetConnectionString(conStringPay), CmdStrPay));
ds.Tables.Add(GetDataFromDatabase(GetConnectionString(conStringTrans), CmdStrTran));

DataTable dtAll = ds.Tables[0].Copy();

for (var i = 1; i < ds.Tables.Count; i++) // this did the trick for 
{                                         // for me with WinForms
    dtAll.Merge(ds.Tables[i]);
}
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dtAll;

// resize column 3 to fit the window content 

WPF.xaml:

<ListView ItemsSource="{Binding DbVersions}" DockPanel.Dock="Top">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Version" DisplayMemberBinding="{Binding Version}" Width="60"/>
            <GridViewColumn Header="Last Updated" DisplayMemberBinding="{Binding LastUpdated}" Width="140"/>
        </GridView>
    </ListView.View>
</ListView>

我试图将ObservableCollection 与类对象一起使用,但没有走远。 任何帮助都感激不尽。

【问题讨论】:

  • 那么现在 DbVersions 是什么类型的集合呢?
  • 我试图采用这种方法 public ObservableCollection _observableCollection = new ObservableCollection() ,如我发布的链接中所示。不确定这是否正确。
  • 你需要使用 ObservableCollection。您的类实现 INotifyPropertyChanged 接口并在其属性集分支上触发事件。我将尝试添加一个简单的示例。
  • 如果您也需要绑定到 DataTable 示例,请告诉我。

标签: c# wpf listview data-binding observablecollection


【解决方案1】:

简单示例:

<Window x:Class="StackSimpleSample.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">
<Grid>
    <ListView ItemsSource="{Binding persons}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

您的 ListView 绑定到在后面的代码中定义的人员,如下所示:

public partial class MainWindow : Window
{
    public ObservableCollection<Person> persons { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        persons = new ObservableCollection<Person>();
        persons.Add(new Person() { Name = "Person1" });
        persons.Add(new Person() { Name = "Person2" });
        persons.Add(new Person() { Name = "Person3" });
        persons.Add(new Person() { Name = "Person4" });
        this.DataContext = this;
    }
}

这是 Person 类:

public class Person : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

所以你必须使用 ObservableCollection&lt;Person&gt; 而不是创建一个包含列和行的 DataTable 作为 ListView 的 Source。

一些提示.. ObservableCollection 将在添加或删除项目时通知视图,并且带有 PropertyChanged 事件的 INotifyPropertyChanged 接口将在集合中现有项目的某些属性发生更改时通知 GUI。

【讨论】:

  • 好吧,我不明白。那么我的 DataSet 需要在哪里使用呢?我已经实现了 INotifyProprtyChanged 接口,但不知道如何将它与我所拥有的绑定在一起。什么将是 people.Add(new Person() { Name = "Person1" });在我的情况下?
  • @Jakubee 使用数据库表中的详细信息,您必须创建模型,我的意思是 c# 类及其信息。就像那个 Person 类。这只是一个例子。您必须实现自己的数据访问层并存储所需的内容。
  • 我不知道该怎么做 ;(( 已经为此浪费了一整天。所以所有的 SQL 查询都应该保留并在 Model 类中使用?你能举个例子说明一下吗?可能看起来?
猜你喜欢
  • 2012-05-26
  • 2018-05-21
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
相关资源
最近更新 更多