Sar*_*els 1 c# wpf xaml listbox itemssource
我不知道我在这里做错了什么.我有一个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();
}
Run Code Online (Sandbox Code Playgroud)
这是我的XAML:
<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
Width="400" Height="300" DisplayMemberPath="Name"
SelectedValuePath="Name"/>
Run Code Online (Sandbox Code Playgroud)
在Template类的一个实例上,有一个Name属性只是一个string.我想要的只是显示模板名称列表.用户不会更改a中的任何数据Template,ListBox只需要是只读的.
模板还有一个Data属性,我稍后会在其中显示ListBox,所以我不想GetTemplates只返回一个字符串列表 - 它需要返回一些Template对象集合.
您无法绑定到方法.使它成为一个属性,它应该工作.
将List设置为DataContext或创建保存列表的ViewModel更好.坦率地说,您可以更好地控制Listbox将绑定到的实例.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
609 次 |
| 最近记录: |