DATA如何绑定字典< string,list< string>>到C#-WPF的列表框? [英] How is it possible to DATA bind a dictionary<string, list<string>> to a listbox in C#-WPF?

查看:69
本文介绍了DATA如何绑定字典< string,list< string>>到C#-WPF的列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>





我尝试过:



i制作了这段代码,但输出结果是---->(收藏)



What I have tried:

i made this code but output came out to be---->(Collection)

推荐答案

Hi


列表框不会自然地绑定到该类型的列表,因为它是多维的。您可以使用 DataTemplate 为列表框执行某些操作,但这并不容易。



如果你的结构略有不同,你就可以得到你想要的东西



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},);
}

返回subjects.ToString()。TrimEnd(',');
}
}

公共覆盖字符串ToString()
{
return
"{subject},"); } return subjects.ToString().TrimEnd(','); } } public override string ToString() { return


{Name} - {SubjectList};
}
}
"{Name} - {SubjectList}"; } }





如果你的Person课程只是持有一个人的详细信息那么你可以有一个列表绑定的人的集合





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





默认情况下,当列表视图显示该集合中的对象时,它将使用 ToString()显示文字。



你应该只有这样的东西来显示信息



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}/>


这篇关于DATA如何绑定字典&lt; string,list&lt; string&gt;&gt;到C#-WPF的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆