开发者

Wpf data bind a listbox to a nested class

开发者 https://www.devze.com 2023-03-05 09:36 出处:网络
So I have the following scenario: I have a class part of a view model as follows: public class ResourceModuleAccess

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></CheckB开发者_开发问答ox>
                            </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.


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.


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; }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消