【问题标题】:Bind collection of objects to ListBox将对象集合绑定到 ListBox
【发布时间】:2012-02-26 10:55:14
【问题描述】:

我有类人:

    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:

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

现在我收集了我的班级并想将其绑定到 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 中看到三个值:一、二、三。我该怎么做?

【问题讨论】:

  • 如果我的收藏是动态创建的,我只是不知道该怎么做。可以给个代码示例吗?

标签: c# .net wpf data-binding


【解决方案1】:

在您的MainWindow 中将您的收藏公开为公共财产。如果您希望 ListBox 在您的集合中添加或删除元素时得到更新,您应该使用 ObservableCollection&lt;T&gt; 而不是 List&lt;T&gt;

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

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

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

<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

附:标识符名称应该标识目的,而不是类型。使用Persons 而不是MyCollection;使用Name 而不是NameProperty

【讨论】:

  • 谢谢。它帮助到我。很好的答案。
【解决方案2】:

使用 ObservableCollection 代替 List。当您在集合中添加/删除项目时,这将更新 UI。获得集合后,将其绑定到 ListBox 的 ItemsSource 属性。

此外,您可以通过覆盖 Person 类的 ToString 方法或在 ListBox 控件上设置 ItemTemplate 来控制 Person 对象在 UI 上的呈现方式。

【讨论】:

  • 但是对于一个简单的下拉菜单,您通常不需要突变。
  • 我在尝试使用ItemSource,但是集合的新元素没有显示在ListBox上,而我没有重新执行listNames.ItemsSource = null; listNames.ItemsSource = myCollection;
  • 如果使用 ObservableCollection,则不必替换 ItemSource。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 2014-05-03
  • 2015-09-01
  • 1970-01-01
  • 2010-12-21
  • 2020-09-28
相关资源
最近更新 更多