Wpf 数据将列表框绑定到嵌套类

发布于 2024-11-07 03:59:28 字数 1843 浏览 0 评论 0原文

所以我有以下场景:

我有一个视图模型的类部分,如下所示:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-11-14 03:59:28

您的 ModulesAccess.ItemsDisplayAccess 列表,因此列表框项目模板的数据上下文将已经是单个 DisplayAccess 对象。

鉴于此,您应该能够在第一个示例中仅绑定到 Name 而不是 DisplayAccess.Name

您的第二个示例也应该可以工作,但您可能需要使用命名空间来限定类型,例如

<DataTemplate DataType="{x:Type yourns:DisplayAccess}">
    ....
</DataTemplate>

顺便说一句,您经常会发现绑定错误被写入 Visual Studio 中的输出窗口(调试/Windows/输出菜单),这可以为您提供一个关于出了什么问题的指针。

Your ModulesAccess.Items is a list of DisplayAccess so your data context for the item template of your listbox will already be a single DisplayAccess object.

Given that, you should be able to just bind to Name rather than DisplayAccess.Name in your first example.

your second example should also work but you might need to qualify the type with a namespace, e.g.

<DataTemplate DataType="{x:Type yourns:DisplayAccess}">
    ....
</DataTemplate>

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.

浅唱々樱花落 2024-11-14 03:59:28

对于有类似问题的人:显示访问字段应该是属性。

 public class DisplayAccess
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private ResourceModule module;

    public ResourceModule Module
    {
        get { return module; }
        set { module = value; }
    }
}

For people with the a similar problem: Display access fields should be properties.

 public class DisplayAccess
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private ResourceModule module;

    public ResourceModule Module
    {
        get { return module; }
        set { module = value; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文