C#中列表框內的列表框

[英]List-box inside a list-box in 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.

當我試圖綁定內部列表框時,我無法這樣做。

The c# code that I tried is as follows.

我嘗試的c#代碼如下。

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

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?

但我無法從xaml.cs代碼訪問listBox2。它說找不到listBox2。另外,我想僅在單擊按鈕時綁定listBox2,而不是在綁定listBox1時綁定。誰能告訴我如何訪問listBox2?

1 个解决方案

#1


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.

您已在listBox1的DataTemplate中添加了listBox2。所以在后面的代碼中無法訪問它。詳細說明,請考慮listBox1中有item1,item2和item3。然后,在item1,item2和item3中將有一個名為listBox2的ListBox對象。您無法在后面的代碼中專門指出其中一個對象。實現綁定的一種方法是,在已綁定到listBox1的項目內創建一個列表,並將其綁定到內部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>

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

注意:請參閱此相關問題 - Windows Phone 8上列表框中的綁定列表框


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/11/725d42d63a3eef161b425364778b5be2.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com