我在一个列表框中有一个列表框,两者都绑定到一个可观察的集合。我已经为这两个重载了SelectionChanged事件。对于嵌套的列表框,我在它的数据模板中有一些标签。我希望能够获得这些标签的内容。这很困难,因为我不能在后台代码中引用它们中的任何一个,即使定义了x:name属性也是如此。有谁有什么想法吗?
<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;子列表框绑定到Enclosure类中的一个可观察集合。
public class Enclosure
{
public ObservableCollection<Server> Slist { get; set; }
...contains other variables as well....
}在应用程序中,它列出了存储模块,每个存储模块都有一个服务器列表。用户可以选择一个包围盒,我可以根据SelectedIndex (我使用ElementAt(SelectedIndex))从Elist中获取包围盒。当我试图从嵌套的列表框中获取一个服务器时,事情会变得更加棘手。我希望能够选择列表中的一个服务器,并从可观察集合Slist中获取该服务器。问题是,当用户直接选择服务器时,我不知道服务器来自Elist中的哪个封装,我无法获得SelectedIndex,因为我无法在>.<背后的代码中引用嵌套列表框中的任何内容。A非常令人沮丧的问题indeed...does有人有什么想法吗?
如果我可以在代码中找到嵌套列表框中的项,这也会很有帮助。
发布于 2012-06-21 05:11:42
下面的示例显示了如何在用户选择子级时获取选定的父级和子级,请参阅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; }
}
}https://stackoverflow.com/questions/11124025
复制相似问题