我有一個列表框內的列表框,並且都綁定到一個可觀察的集合。我已經重載了兩者的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,因爲我無法在後面的代碼中引用嵌套列表框中的任何內容>。 <確實是一個非常令人沮喪的問題......有沒有人有任何想法?
如果我可以在代碼中得到嵌套列表框中的項目,這也會有幫助。
甚至一種方式來獲得在嵌套列表框,從可觀察集合的項目? – KateMak