我不知道我在这里做错了什么。我有一个ListBox
whoDataContext
和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
. 我想要的只是显示模板名称列表。用户不会更改 aTemplate
中的任何数据,ListBox
只需要只读即可。
模板还有一个Data
属性,稍后我将在 this 中显示ListBox
,所以我不想GetTemplates
只返回一个字符串列表——它需要返回一些Template
对象集合。