【问题标题】:Get ObservableCollection from ListBox从 ListBox 获取 ObservableCollection
【发布时间】:2012-07-16 15:01:57
【问题描述】:

有没有办法以编程方式从 ListBox 确定关联的 ObservableCollection?

我在我的 XAML 中将 ItemsSource 绑定到 ListBox,当用户从其中选择一个项目时,我希望程序不仅能找到该项目来自哪个 ListBox(我已经设法做到了),而且还能得到与之关联的 ObservableCollection。

我认为通过使用 sourceContainer.ItemsSource.ToString()(其中 sourceContainer 是通过编程找到的 ListBox),我可以确定它背后的 DataBound ObservableCollection。然而,事实并非如此。我错过了什么?

谢谢!

编辑:这里有一些关于创建 ObservableCollections 以及我如何确定选择哪个框的代码。 (我知道这可能不是最好的方法,我还在学习中......)

首先是 XAML(每个 ListBox 都有这一行用于常规绑定):

ItemsSource="{Binding Path=ListOneItems}"

现在是创建 ObservableCollections 的代码:

    private ObservableCollection<DataItem> listOneItems;

    public ObservableCollection<DataItem> ListOneItems
    {
        get
        {
            if (listOneItems == null)
            {
                listOneItems = new ObservableCollection<DataItem>();
            }
            return listOneItems;
        }
    }

程序在此处确定所选项目的父项:

//Finds the name of the container that holds the selected SLBI (similar to finding the initial touched object)
FrameworkElement determineSource = e.OriginalSource as FrameworkElement;
SurfaceListBox sourceContainer = null;

while (sourceContainer == null && determineSource != null)
{
    if ((sourceContainer = determineSource as SurfaceListBox) == null)
    {
        determineSource = VisualTreeHelper.GetParent(determineSource) as FrameworkElement;
    }
}

//Name of the parent container
strParentContainer = sourceContainer.Name;

//Name of the ObservableCollection associated with parent container?

如果还需要什么,请告诉我。

【问题讨论】:

  • 假设你不太明白你需要什么。如果您订阅的列表框选择发生了变化,您可以从发件人对象中获取列表框。 var listbox=(列表框)发件人;而不是获取 listbox.ItemsSource 以获取关联的集合

标签: c# wpf data-binding listbox observablecollection


【解决方案1】:

如果您已直接绑定到 ObservableCollection&lt;T&gt; 的属性,您应该可以这样做:

var col = (ObservableCollection<MyClass>)sourceContainer.ItemsSource;

如果不是这样,您需要发布相关的 XAML 和代码。

【讨论】:

  • 我已经尝试过您的建议,但由于某种原因它仍然无法正常工作。当这条线被击中时,它只是读取“Count = 1”。我希望让它读取集合的实际名称。如果我所说的没有意义,请随时纠正我。数据绑定对我来说仍然很陌生。
  • 这听起来像你得到一个包含 1 项的列表 (ObservableCollection)?你说的名字是什么意思?您应该只关心集合的内容,而不是集合的名称......这似乎是一个 UI 问题,并且与您的业务实现有一些耦合,可能不应该存在
【解决方案2】:

Eren's answer 是正确的,您可以通过将ItemsSource 属性转换为正确的集合类型来获取实际集合。

ObservableCollection<DataItem> sourceCollection = 
    sourceContainer.ItemsSource as ObservableCollection<DataItem>;

如果您只想要评论中建议的 Name 属性,您可以获取 ItemsSourcePropertyBinding 并查看 Path 属性

var binding = soureContainer.GetBindingExpression(ListBox.ItemsSourceProperty);
var sourceCollectionName = binding.ParentBinding.Path.Path;

【讨论】:

  • 感谢您解决问题;我现在可以明白为什么人们不会特别关心 ObservableCollection 的名称,因为它除了名称之外还有更多有用的功能。我希望我可以将这两个都设置为最佳答案!
  • @NateFrueh 我想知道你到底想要什么Name 属性:)
【解决方案3】:

您与列表框关联的集合是 ObservableCollection?因为如果你设置了一个数组 - 它不会在后台魔术转换为 ObservableCollection。

告诉你为什么需要这个逻辑。假设 smth 在其他逻辑中是错误的。

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 2011-01-07
    • 2012-08-03
    • 2013-04-25
    • 2023-03-31
    • 2018-09-11
    • 2019-02-09
    • 1970-01-01
    • 2013-06-11
    相关资源
    最近更新 更多