我想了解来自 msdn 的示例(http://msdn.microsoft.com/en-us/library/ms742521.aspx#defining_simple_datatemplate)。
XAML 代码:
<ListBox Width="400" Margin="10" ItemsSource="{Binding Source={StaticResource MyTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
文本块中的数据是对象的属性,对吗?对象在代码隐藏文件中的列表(?)myTodoList 中?
protected ObservableCollection<TODO> _myTodoList= new ObservableCollection<TODO>();
public ObservableCollection<TODO> MyTodoList
{
get { return _myTodoList; }
}
添加待办事项
TODO t1 = new TODO();
t1.TaskName = "TaskName1";
t1.Description = "Description1";
t1.Priority = "Priority1";
_myTodoList.Add(t1);
TODO t2 = new TODO();
t2.TaskName = "TaskName2";
t2.Description = "Description2";
t2.Priority = "Priority2";
_myTodoList.Add(t2);
我的测试 TODO 类:
public class TODO
{
public string TaskName { get; set; }
public string Description { get; set; }
public string Priority { get; set; }
}
但是代码抛出异常......
怎么了?