【发布时间】:2011-12-19 15:06:40
【问题描述】:
嘿,我需要访问我在 WPF 中创建的列表框中的项目。我现在拥有的是用户可以将一个新的类对象添加到 ListBox.ItemsSource 绑定到的 ObservableCollection。这个类中有数据,这些数据用于组合框、文本框和其他类似的简单事物中。
我所做的实际上是基于 windowsclient.net 的一个 tut 视频
//Here's What I have so far
<ListBox x:Name="MyList" Width = "300" Height = 300">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="ItemName"
Text={Binding Path=PersonName}/>
<ComboBox x:Name="NumberOfRes"
SelectedIndex="0"
SelectionChanged="NumberOfRes_SelectionChanged"
ItemsSource={Binding Path=ListOfNums}/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//ListOfNums
private ObservableCollection<int> _listofNums =
new ObservableCollection<int>()
{ 1,2,3,4,5,6,7,8 };
public ObservableCollection<int> ListOfNums
{
get{return _listOfNums;}
}
//class Resident
public class Resident
{
public string personName {get;set;}
}
所以我有这个工作。我有一个添加新类对象的按钮,每个对象都通过数据绑定正确设置了数据。因此,当添加新对象时,我可以看到我的默认 personName,并且我有一个填充了 ListOfNums 整数的组合框。
我的问题是我不知道如何访问这些“子对象”(请原谅我是 WPF 新手,不知道正确的术语)正在选择什么。我想知道当引发 selectionChange 事件时我的组合框的 SelectedItem 是什么,或者当用户在文本框中键入内容时我的新 personName 设置为什么。因此,用户在 listBox 中选择这些 ItemTemplate 对象之一,并且该项目具有组合框和用户单击的其他 UI 控件。当用户单击这些 UI 控件之一时,会发生一些数据更改。
我尝试了((Resident)MyList.SelectedItem).personName 和其他几种方法,只是为了看看我是否可以访问数据并对其进行操作。 Now the funny thing is that my comboBox in the ItemTemplate has a selection changed event that is called when the selection is changed, but I can't access the data!
提前致谢!
【问题讨论】:
标签: c# wpf data-binding combobox listbox