Wpf 数据将列表框绑定到嵌套类
所以我有以下场景:
我有一个视图模型的类部分,如下所示:
public class ResourceModuleAccess
{
public class ModuleAccess
{
ResourceModule module;
Mode mode;
}
public List<DisplayAccess> Items
{
get
{
var result = from g in groups
join p in groupAccess on g.GroupID equals p.GroupId into outer
from p in outer.DefaultIfEmpty()
select new DisplayAccess { Name = g.Name, Module = (p == null) ? ResourceModule.None : p.Module };
var output = result.ToList();
return output;
}
}
并且我正在尝试将项目数据绑定到列表框以显示名称和模块
<ListBox ItemsSource="{Binding ModulesAccess.Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding DisplayAccess.Name}"></Label>
<CheckBox></CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
列表框正确显示项目,因此它找到了集合,但是它无法将属性映射到
我也尝试过的
<DataTemplate DataType="{x:Type DisplayAccess}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"></Label>
<CheckBox></CheckBox>
</StackPanel>
</DataTemplate>
标签,但无法构建:它说找不到公共类型 DisplayAccess。
So I have the following scenario:
I have a class part of a view model as follows:
public class ResourceModuleAccess
{
public class ModuleAccess
{
ResourceModule module;
Mode mode;
}
public List<DisplayAccess> Items
{
get
{
var result = from g in groups
join p in groupAccess on g.GroupID equals p.GroupId into outer
from p in outer.DefaultIfEmpty()
select new DisplayAccess { Name = g.Name, Module = (p == null) ? ResourceModule.None : p.Module };
var output = result.ToList();
return output;
}
}
and I am trying to databind the items to a listbox to display the name and module
<ListBox ItemsSource="{Binding ModulesAccess.Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding DisplayAccess.Name}"></Label>
<CheckBox></CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The listbox correctly displays for items, so it found the collection, but it is not able to map the properties to the label
I also tried
<DataTemplate DataType="{x:Type DisplayAccess}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"></Label>
<CheckBox></CheckBox>
</StackPanel>
</DataTemplate>
but that doesn't build: it says it cannot find the public type DisplayAccess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
ModulesAccess.Items
是DisplayAccess
列表,因此列表框项目模板的数据上下文将已经是单个DisplayAccess
对象。鉴于此,您应该能够在第一个示例中仅绑定到
Name
而不是DisplayAccess.Name
。您的第二个示例也应该可以工作,但您可能需要使用命名空间来限定类型,例如
顺便说一句,您经常会发现绑定错误被写入 Visual Studio 中的输出窗口(调试/Windows/输出菜单),这可以为您提供一个关于出了什么问题的指针。
Your
ModulesAccess.Items
is a list ofDisplayAccess
so your data context for the item template of your listbox will already be a singleDisplayAccess
object.Given that, you should be able to just bind to
Name
rather thanDisplayAccess.Name
in your first example.your second example should also work but you might need to qualify the type with a namespace, e.g.
As an aside, you will often find binding errors are written to the Output window in visual studio (Debug / Windows / Output menu) which can give you a pointer as to what's going wrong.
对于有类似问题的人:显示访问字段应该是属性。
For people with the a similar problem: Display access fields should be properties.