2012-06-20 21 views
0

我有一個列表框內的列表框,並且都綁定到一個可觀察的集合。我已經重載了兩者的SelectionChanged事件。對於嵌套列表框,我的數據模板中有一些標籤。我希望能夠獲得這些標籤的內容。這很困難,因爲我不能在後面的代碼中引用它們中的任何一個,即使定義了x:name屬性。有人有主意嗎?從NESTED列表框中綁定到可觀察集合的標籤中獲取內容?

<ListBox Grid.Row="5" x:Name="lb1" ItemsSource="{Binding}" DataContext="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lb1_SelectionChanged"> 

     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Label x:Name="txtEnclosure" Content="{Binding Path=EnclosureID}"/> 
       <......other labels bound to other properties...> 
       <ListBox x:Name="lbserver" ItemsSource="{Binding Path=Slist}"> 
         <ListBox.ItemsPanel> 
          <ItemsPanelTemplate> 
           <WrapPanel/> 
          </ItemsPanelTemplate> 
         </ListBox.ItemsPanel> 

         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <Label x:Name="txtSlot" Content="{Binding Path=Slot}" /> 
           <Label x:Name="txtServer" Content="{Binding Path=HostnameID}" /> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

父列表框結合稱爲ELIST可觀察集合(外殼的可觀察集合,我定義的類)

this.DataContext = Settings.Elist; 

而且孩子列表框綁定到可觀察集合的外殼防護等級的內。

public class Enclosure 
{ 
    public ObservableCollection<Server> Slist { get; set; } 
    ...contains other variables as well.... 
} 

在應用程序中,它列出了機箱,並且每個機箱都有一個服務器列表。用戶可以選擇一個機箱,我可以根據SelectedIndex(我使用ElementAt(SelectedIndex))從Elist獲取機箱。當我嘗試從嵌套列表框中獲取其中一個服務器時,事情會變得更加棘手。我希望能夠選擇列表中的一臺服務器,並從可觀察集合Slist獲取服務器。問題是,當用戶直接選擇服務器時,我不知道服務器來自Elist的哪個機箱,並且我無法獲取SelectedIndex,因爲我無法在後面的代碼中引用嵌套列表框中的任何內容>。 <確實是一個非常令人沮喪的問題......有沒有人有任何想法?

如果我可以在代碼中得到嵌套列表框中的項目,這也會有幫助。

+0

甚至一種方式來獲得在嵌套列表框,從可觀察集合的項目? – KateMak

回答

1

下面的示例顯示了當用戶選擇一個孩子時如何獲得選定的父和子,請參閱OnSelectedModelChanged方法。

XAML:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <StackPanel> 
     <ListBox ItemsSource="{Binding .}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/> 
         <ListBox ItemsSource="{Binding Path=Models}" SelectionChanged="OnSelectedModelChanged"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding Path=Name}" /> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
</Window> 

後面的代碼:

using System.Windows; 
using System.Windows.Controls; 
using System.ComponentModel; 
using System.Collections.Generic; 
using System.Windows.Controls.Primitives; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      List<Make> cars = new List<Make>(); 
      cars.Add(new Make("Ford") { Models = new List<Model>() { new Model("F150"), new Model("Taurus"), new Model("Explorer") } }); 
      cars.Add(new Make("Honda") { Models = new List<Model>() { new Model("Accord"), new Model("Pilot"), new Model("Element") } }); 
      DataContext = cars; 
     } 

     private void OnSelectedModelChanged(object sender, SelectionChangedEventArgs e) 
     { 
      Selector modelSelector = sender as Selector; 
      Model selectedModel = modelSelector.SelectedItem as Model; 
      Make selectedMake = modelSelector.DataContext as Make; 
     } 
    } 

    public class Make 
    { 
     public Make(string name) 
     { 
      Name = name; 
     } 

     public string Name { get; private set; } 
     public IEnumerable<Model> Models { get; set; } 
    } 

    public class Model 
    { 
     public Model(string name) 
     { 
      Name = name; 
     } 
     public string Name { get; private set; } 
    } 
} 
+0

謝謝!!!!!!!!!!!!!!! – KateMak

+0

你真的救了我的命。試圖弄清楚這件事,我一直在靠牆敲打我的頭。 – KateMak

相關問題