//嗨!我需要把我的数据放到有多个列的listBox中我看到了这个链接stackoverflow.com...,但它谈到了所有事情,但没有提到我可以将项目添加到列中的方法,请您解释一下如何将数据项添加到列中,非常感谢。我成功地完成了以下工作
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="1" Width="100" DisplayMemberBinding="{Binding Path=Field1}" />
<GridViewColumn Header="2" Width="100" DisplayMemberBinding="{Binding Path=Field2}" />
<GridViewColumn Header="3" Width="100" DisplayMemberBinding="{Binding Path=Field3}" />
</GridView.Columns>
</GridView>
</ListView.View>`
public sealed class MyListBoxItem
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
}
public sealed class MyViewModel
{
public ObservableCollection<MyListBoxItem> Items { get; private set; }
public MyViewModel()
{
Items = new ObservableCollection<MyListBoxItem>();
Items.Add(new MyListBoxItem { Field1 = "One", Field2 = "Two", Field3 = "Three" });
}
}发布于 2012-06-18 23:19:49
您需要在Window1.xaml.cs类的构造函数内设置包含ListBox控件的Window (假设它是Window1)的DataContext属性,如下所示:
public Window1()
{
MyViewModel vm = new MyViewModel();
this.DataContext = vm;
}下一步是将ListBox控件(在XAML中)的ItemsSource属性设置为您在ViewModel类中提供的Items属性:
<ListBox ItemsSource="{Binding Path=Items}">
<!--Other XAML-->
</ListBox>此外,您还应该为您的MyListBoxItem类实现INotifyPropertyChanged接口,here in MSDN对此进行了说明。这是因为在您的WPF应用程序中实现了MVVM模式。要求您实现oneway或twoway数据绑定的更改通知才能工作(请参阅本文以了解有关DataBinding的更多信息)。
下面是对MVVM on MSDN的更详细的解释。
https://stackoverflow.com/questions/11085463
复制相似问题