c# - 访问列表框中的数据

标签 c# wpf data-binding combobox listbox

嘿,我需要访问我在 WPF 中创建的列表框中的项目。我现在拥有的是用户可以将一个新的类对象添加到 ListBox.ItemsSource 绑定(bind)到的 ObservableCollection。此类中包含数据,其中数据用于组合框、文本框和其他类似的简单内容。

我所做的实际上是基于来自 windowsclient.net 的视频之一

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

所以我有那个工作。我有一个添加新类对象的按钮,每个对象都通过数据绑定(bind)正确设置了数据。因此,当添加新对象时,我可以看到我的默认 personName,并且我有一个组合框,其中填充了来自 ListOfNums 的整数。

但我的问题是我不知道如何访问这些“子对象”(请原谅我是 WPF 新手,不知道正确的术语)正在选择什么。我想知道在引发 selectionChange 事件时我的组合框的 SelectedItem 是什么,或者当用户在文本框中键入内容时我的新 personName 设置为什么。因此,用户在 listBox 中选择这些 ItemTemplate 对象之一,并且该项目具有用户单击的组合框和其他 UI 控件。当用户单击其中一个 UI 控件时,一些数据会发生变化。

我已经尝试了 ((Resident)MyList.SelectedItem).personName 和其他一些方法,只是为了看看我是否可以访问数据并对其进行操作。现在有趣的是,我在itemTemplate中的combobox具有选择更改时的选择更改的事件,但我无法访问数据!

!

提前致谢!

最佳答案

有多种方法可以实现您的目标。最简单的方法是在您的 ViewModel 上放置一个 SelectedItem 属性。

您的 VieWModel 将如下所示...

public class MyViewModel : INotifyPropertyChanged
{
      ObservableCollection<Resident> Residents { get; }

      private Resident _selectedItem;
      public Resident SelectedItem
      {
           get { return _selectedItem; }
           set
           {
                _selectedItem = value;
                PropertyChangedEventHandler handler = PropertyChanged;
                if(handler != null)
                    handler(this, new PropertyChangedEventArgs("SelectedItem");
           }
      }
}

在您的 View 中,将 DataContext 设置为此 ViewModel 的一个实例,其中您的 ListBox.ItemsSource 绑定(bind)到 Residents 并且您的 ListBox.SelectedItem 已绑定(bind)到 SelectedItem

<ListBox ItemsSource="{Binding Path=Residents}" 
         SelectedItem="{Binding Path=Selectedtem, Mode=TwoWay}" />

如果您想在选择更改时执行操作,请注意您的 SelectedItem 属性的 setter ,它将包含新选择的项目。

编辑:

刚刚注意到您的 Resident 模型没有实现 INotifyPropertyChanged,它需要这样做才能将更改传播到下游。然后,您可以获得数据将在给定属性的 setter 中更改的点,如 SelectedItem 属性所述。

关于c# - 访问列表框中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969175/

相关文章:

c# 使用 WebClient 下载文件并保存

c# - 如何获取 UI 元素的屏幕位置?

c# - 使用 ProcessStartInfo 为 UAC 重定向标准输出和提示

wpf - 与 ElementName 绑定(bind)不起作用

c# - 将项目移动到列表顶部时,WPF AlternationIndex 会回绕

c# - 异步填充强类型数据集

c# - 我可以将 using() {} block 与方法的 out 参数结合使用吗?

c# - 在 LINQ 中使用求和方法

c# - 从开关中的字符串名称返回等效对象

asp.net - 除 MS-SQL 外,是否有任何适用于数据库的 linq-to-sql?