首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ObservableCollection将ObservableCollection绑定到ListView

使用ObservableCollection将ObservableCollection绑定到ListView
EN

Stack Overflow用户
提问于 2015-06-17 11:15:46
回答 1查看 2.6K关注 0票数 1

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

下面是我的WPF和WinForms代码:

WinForms:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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,但没有取得多大进展。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2015-06-17 11:48:30

简单的例子:

代码语言:javascript
复制
<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绑定到在下面代码中定义的人员:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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<Person>,而不是创建一个以列和行作为ListView源的DataTable。

一些暗示..。ObservableCollection将在添加或删除项时通知视图,当集合中存在的项的某些属性发生更改时,INotifyPropertyChanged接口及其PropertyChanged事件将通知GUI。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30889955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档