C#中列表框内的列表框 [英] List-box inside a list-box in C#

查看:28
本文介绍了C#中列表框内的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的列表框中有一个列表框.

I have a list-box inside a list-box like this.

<ListBox x:Name="listBox1">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel>
            <StackPanel.Background>
               <SolidColorBrush Color="#FF2B3643" Opacity="1"/>
            </StackPanel.Background>
            <StackPanel>
               <TextBlock Text="{Binding X}"/>
               <TextBlock Text="{Binding Y}"/>
            </StackPanel>
            <Button Click="Button_Click">
            <ListBox x:Name="listBox2">
               <ListBox.ItemTemplate>
                  <DataTemplate>
                     <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Number}"/>
                     </StackPanel>
                  </DataTemplate>
               </ListBox.ItemTemplate>
            </ListBox>
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

当我尝试绑定内部列表框时,我无法这样做.

When I tried to bind the inner list-box, I wasn't able to do so.

我试过的c#代码如下.

The c# code that I tried is as follows.

List<Person> p = new List<Person>();

Person student = new Person();
student.Name = "Name1";
student.Number = "Number1";
p.Add(student);

Person teacher = new Person();
teacher.Name = "Name2";
teacher.Number = "Number2";
p.Add(teacher);

listBox2.ItemsSource = p; // cannot access listBox2

但我无法从 xaml.cs 代码访问 listBox2.它说找不到 listBox2.此外,我只想在单击按钮时绑定 listBox2,而不是在绑定 listBox1 时绑定.有人能告诉我如何访问 listBox2 吗?

But I cannot access listBox2 from the xaml.cs code. It says listBox2 is not found. Also I want to bind the listBox2 only when the button is clicked and not when binding listBox1. Can someone tell me how do I access listBox2?

推荐答案

您在 listBox1 的 DataTemplate 中添加了 listBox2.所以在后面的代码中将无法访问它.详细说明,假设您在 listBox1 中有 item1、item2 和 item3.然后您将在 item1、item2 和 item3 中拥有一个名为 listBox2 的 ListBox 对象.您不能在后面的代码中明确指出这些对象之一.实现绑定的一种方法是,在绑定到 listBox1 的项目内创建一个列表,并将其绑定到内部 ListBox.

You have added listBox2 inside the DataTemplate of listBox1. So it won't be accessible in the code behind. To elaborate, consider you have item1, item2 and item3 in listBox1. Then you will have an object of ListBox named listBox2 in item1, item2 and item3. You cannot specifically point one of these objects in the code behind. One way to achieve the binding is, you create a list inside the items that you have bound to listBox1 and bind it to the inner ListBox.

List<Item> MainList = new List<Item>();

class Item
{
   List<Person> PersonList = new List<Person>();
}


<ListBox x:Name="listBox1" ItemsSource = "{Binding MainList}">
   <ListBox.ItemTemplate>
      ...
            <ListBox x:Name="listBox2" ItemsSource= "{Binding PersonList}">
               <ListBox.ItemTemplate>
                  ...
               </ListBox.ItemTemplate>
            </ListBox>
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

注意:请参阅此相关问题 - 绑定列表框在 Windows Phone 8 上的列表框中

Note: Refer this related question - Binding listbox in listbox on Windows Phone 8

这篇关于C#中列表框内的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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