将对象集合绑定到ListBox [英] Bind collection of objects to ListBox

查看:66
本文介绍了将对象集合绑定到ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上课的人:

    public class Person : INotifyPropertyChanged
    {

        private String name = "";
        public event PropertyChangedEventHandler PropertyChanged;

        public Person()
        {
            NameProperty = "hi";
        }

        public Person(String _name)
        {
            NameProperty = _name;
        }

        public String NameProperty
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("NameProperty");
            }
        }

        public override String ToString()
        {
            return NameProperty;
        }

        protected void OnPropertyChanged(string name)
        {

            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }

        }

    }

我可以将我的类的实例绑定到TextBox:

And I can bind the instance of my class to TextBox:

myPerson = new Person("demas");
Binding myBinding = new Binding("NameProperty");
myBinding.Source = myPerson;
txtName.SetBinding(TextBox.TextProperty, myBinding);

现在我已经收集了我的课程,并希望将其绑定到ListBox:

Now I have collection of my class and want to bind it to ListBox:

List<Person> myCollection = new List<Person>(); // on the MainWindow level

myCollection.Add(new Person("one"));
myCollection.Add(new Person("two"));
myCollection.Add(new Person("three"));

所以我必须在ListBox中看到三个值:1、2、3.我该怎么办?

So I have to see in the ListBox three values: one, two, three. How can I do it?

推荐答案

MainWindow 中将您的集合公开为公共属性.如果您希望在将元素添加或删除到集合时更新 ListBox ,则应使用 ObservableCollection< T> 而不是 List< T> .

Expose your collection as a public property in your MainWindow. If you want your ListBox to get updated when elements are added or removed to your collection, you should use an ObservableCollection<T> rather than List<T>.

readonly ObservableCollection<Person> myCollection = new ObservableCollection<Person>();

public ObservableCollection<Person> MyCollection
{
    get { return myCollection; }
} 

然后,在您的XAML中,使用数据绑定来引用属性:

Then, in your XAML, use data binding to reference the property:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication1"
        xmlns:p="clr-namespace:WpfApplication1.Properties"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding MyCollection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding NameProperty}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

请不要忘记在您的 ListBox 或其父窗口中设置 DataContext .

Don’t forget to set the DataContext either on your ListBox or on its parent window.

P.S.标识符名称应标识目的,而不是类型.使用 Persons (而不是 MyCollection );使用 Name 代替 NameProperty .

P.S. Identifier names should identify the purpose, not the type. Use Persons instead of MyCollection; use Name instead of NameProperty.

这篇关于将对象集合绑定到ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆