简体   繁体   English

从绑定到可观察集合的NESTED列表框中的标签中获取内容?

[英]Get content from the labels in a NESTED listbox bound to an observable collection?

I have a listbox inside a listbox, and both are binded to an observable collection. 我在一个列表框内有一个列表框,并且两者都绑定到一个可观察的集合。 I have overloaded the SelectionChanged event for both. 我都重载了SelectionChanged事件。 For the nested listbox, I have a few labels in its data template. 对于嵌套列表框,我在其数据模板中有一些标签。 I want to be able to get the content of those labels. 我希望能够获得这些标签的内容。 It's just difficult because I cannot refer to any of them in the code behind, even with the x:name property defined. 这很困难,因为即使定义了x:name属性,我也无法在后面的代码中引用它们中的任何一个。 Anyone have any ideas? 有人有想法么?

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

The parent listbox binds to an observable collection called Elist (an observable collection of Enclosures, a class I defined) 父列表框绑定到称为Elist的可观察集合(Eclosure的可观察集合,我定义了一个类)

this.DataContext = Settings.Elist;

And the child listbox binds to an observable collection inside of the Enclosure class. 并且子列表框绑定到Enclosure类内部的一个可观察集合。

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

In the application, it lists enclosures, and each enclosure has a list of servers. 在应用程序中,它列出了机箱,每个机箱都有一个服务器列表。 The user can select an Enclosure, and I can get the Enclosure from Elist based on the SelectedIndex (I use ElementAt(SelectedIndex)). 用户可以选择一个附件,然后我可以基于SelectedIndex从Elist获取附件(我使用ElementAt(SelectedIndex))。 Things just get much more tricky when I try to get one of the Servers from the nested listbox. 当我尝试从嵌套列表框中获取其中一台服务器时,事情变得更加棘手。 I want to be able to select one of the servers in the list and get the Server from the observable collection Slist. 我希望能够选择列表中的服务器之一,并从可观察的集合Slist中获取服务器。 The problem is that when the user selects the server directly, I don't know which Enclosure from Elist the server is from, aaand I can't get the SelectedIndex because I can't refer to anything from the nested listbox in the code behind >.< A very frustrating problem indeed...does anyone have any ideas? 问题是,当用户直接选择服务器时,我不知道服务器来自Elist的哪个机柜,而且我无法获取SelectedIndex,因为我无法从背后代码中的嵌套列表框中引用任何内容>。<实际上,这是一个非常令人沮丧的问题……有人有任何想法吗?

If I can get at the items in the nested listbox in code that would be helpful as well. 如果我可以通过代码获得嵌套列表框中的项目,那也将有所帮助。

Below sample shows how to get selected parent and child when user selects a child, see OnSelectedModelChanged method. 下面的示例演示如何在用户选择子项时获取选定的父项和子项,请参阅OnSelectedModelChanged方法。

XAML: 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>

Code behind: 后面的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM