【发布时间】:2013-10-01 10:45:17
【问题描述】:
我有一个像这样的对象:
class RCLocation : INotifyPropertyChanged
{
private string _id;
private string _name;
private bool _checked;
public string Id { /* get/set with NotifyPropertyChanged() */ }
public string Name { /* get/set with NotifyPropertyChanged() */ }
public bool Checked { /* get/set with NotifyPropertyChanged() */ }
/* INotifyPropertyChanged implementation methods */
}
现在在我的 MainWindow.xaml 中有一个 ItemsControl,如下所示:
<ItemsControl Name="lstDropOff" ScrollViewer.VerticalScrollBarVisibility="Auto">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Checked, Mode=TwoWay}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我在后面的代码中将数据绑定到这个列表,如下所示:
ObservableCollection<RCLocation> dropOffs = new ObservableCollection<RCLocation>();
lstDropOff.ItemsSource = dropOffs;
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });
我刚刚添加的项目没有显示在 ItemsControl 中。我到底做错了什么?想不通:/
感谢您的帮助。
【问题讨论】:
-
你没有得到行来显示数据,或者你甚至没有得到行吗?
-
我什至没有得到任何行,我在控件上有一个空的数据模板并且保留在那里,所以控件看不到任何项目...
-
您还用 ItemsControl 做什么?我使用您提供的代码创建了一个干净的示例应用程序,它可以工作。您是否有机会尝试从后台/工作线程更新 ItemsSource?
-
它现在正在工作,显然我只是很愚蠢,我在一个忘记调用的函数中创建绑定......
标签: c# wpf xaml data-binding observablecollection