Click here to Skip to main content
15,506,865 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
namespace WPFDataBinding
{
    public partial class MainWindow : Window
    {
        public Person Obj { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Obj = new Person();
            List<string> subjects1 = new List<string>();
            subjects1.Add("C");
            subjects1.Add("C++");
            subjects1.Add("C#");
            List<string> subjects2 = new List<string>();
            subjects2.Add("JAVA");
            subjects2.Add("JS");
            subjects2.Add("CSS");
            Obj.studDetail.Add("Kush", subjects1);
            Obj.studDetail.Add("Yash", subjects2);
            DataContext = this;
        }
        public class Person
        {
            private Dictionary<string, List<string>> StudDetail = new Dictionary<string, List<string>>();
            public Dictionary<string, List<string>> studDetail
            {
                get { return StudDetail; }
                set { StudDetail = value; }
            }}}}
WPF...
<Window> 
        Title="MainWindow" Height="350" Width="525">
    <ListBox Background="Gray">
        <ItemsControl x:Name="dictionaryItemsControl" ItemsSource="{Binding Person}">
            <Label Content="{Binding Obj}">
                <Label.ContentTemplate>
                    <DataTemplate>
                       <Label Name="Sub1" FontSize="30" Content="{Binding studDetail}"
                               Margin="2" Height="Auto" Width="Auto" />
                    </DataTemplate>
                </Label.ContentTemplate>
            </Label>
        </ItemsControl>
    </ListBox>
</Window>


What I have tried:

i made this code but output came out to be---->(Collection)
Posted
Updated 18-Jul-18 5:54am

1 solution

Hi
The list box won't naturally bind to that type of list as it is multidimensional. You might be able to do something with a DataTemplate for the list box but it wouldn't be easy.

If you structured slightly differently you would be able to get what you want

public class Person
{
    public string Name { get; set; }

    public List<string> subjects { get; set; } = new List<string>();

    public string SubjectList
    {
        get
        {
            StringBuilder subjects = new StringBuilder();

            foreach (string subject in Subjects)
            {
                subjects.Append($"{subject},");
            }

            return subjects.ToString().TrimEnd(',');
        }
    }

    public override string ToString()
    {
        return $"{Name} - {SubjectList}";
    }
}


If you have your Person class just holding the details of one person then you can have have a collection of people that your list is bound to

public List<Person> Obj {get; set;}


By default, when the list view comes to display the objects within that collection it will use ToString() to display the text.

You should only something like this to display the information
<ListBox Background="Gray"
               ItemSource="{Binding Obj}/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900