【发布时间】:2010-08-17 14:18:43
【问题描述】:
我不知道我在这里做错了什么。我有一个ListBox,其DataContext 和ItemsSource 已设置,但是当我运行我的应用程序时ListBox 中没有任何内容。调试时,我为ListBox 获取项目的方法的第一行永远不会被命中。这是我所拥有的:
// Constructor in UserControl
public TemplateList()
{
_templates = new Templates();
InitializeComponent();
DataContext = this;
}
// ItemsSource of ListBox
public List<Template> GetTemplates()
{
if (!tryReadTemplatesIfNecessary(ref _templates))
{
return new List<Template>
{
// Template with Name property set:
new Template("No saved templates", null)
};
}
return _templates.ToList();
}
这是我的 XAML:
<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
Width="400" Height="300" DisplayMemberPath="Name"
SelectedValuePath="Name"/>
在Template 类的实例上,有一个Name 属性,它只是一个string。我想要的只是显示模板名称列表。用户不会更改Template 中的任何数据,ListBox 只需要只读即可。
模板还有一个Data 属性,稍后我将在这个ListBox 中显示它,所以我不想让GetTemplates 只返回一个字符串列表——它需要返回一些@ 集合987654336@ 个对象。
【问题讨论】:
标签: c# wpf xaml listbox itemssource