WPF 合并项目源列表

发布于 2024-09-09 03:16:01 字数 794 浏览 6 评论 0原文

我正在后端类中处理 2 个列表。每个列表都是不同的类型。希望向用户呈现一个列表(包含两个列表的并集),当选择此列表中的项目时,会显示该项目的详细信息。

代码看起来像这样:

我的后端类看起来像这样

public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}

我的 XAML 看起来像这样

<ListBox x:Name="TheListBox" ItemsSource={Some Expression to merge People and Products}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

有什么建议吗?

I am working with a 2 lists in a backend class. Each list is a different type. Would like to present the user with a single list (containing a union of both lists) of which when an item in this list is selected the item's details appear.

The code will look something like:

My backend class looks somethings like this

public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}

My XAML Looks Something Like This

<ListBox x:Name="TheListBox" ItemsSource={Some Expression to merge People and Products}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少情深 2024-09-16 03:16:01

为此,您可以使用 CompositeCollection。看看这个问题

You can use a CompositeCollection for this. Have a look at this question.

流年已逝 2024-09-16 03:16:01

我不明白为什么你不只是在 ViewModel 中公开这样的属性:

ObservableCollection<object> Items 
{
  get 
  {
    var list = new ObservableCollection<object>(People);
    list.Add(Product);
    return list;
  }
}

然后在你的 xaml 中执行以下操作:

<ListBox x:Name="TheListBox" ItemsSource={Binding Items}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

更新:

如果你需要以不同的方式操作你的模型,请执行以下操作:

ObservableCollection<object> _Items 
ObservableCollection<object> Items 
{
  get 
  {
    if (_Items == null)
    {
      _Items = new ObservableCollection<object>();
      _Items.CollectionChanged += EventHandler(Changed);
    }
    return _Items;
  }
  set 
  { 
    _Items = value;
    _Items.CollectionChanged += new CollectionChangedEventHandler(Changed);
  }
}

void Changed(object sender,CollectionChangedEventArgs e)
{
  foreach(var item in e.NewValues)
  {
    if (item is Person)
      Persons.Add((Person)item);
    else if (item is Product)
      Products.Add((Product)item);
  }
}

这只是一个例子。但如果您修改上述内容以满足您的需求,它可能会帮助您实现目标

I don't understand why you don't just have expose a property like this in your ViewModel:

ObservableCollection<object> Items 
{
  get 
  {
    var list = new ObservableCollection<object>(People);
    list.Add(Product);
    return list;
  }
}

and then in your xaml you do this:

<ListBox x:Name="TheListBox" ItemsSource={Binding Items}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

UPDATE:

If you need to manipulate your model differently do the following:

ObservableCollection<object> _Items 
ObservableCollection<object> Items 
{
  get 
  {
    if (_Items == null)
    {
      _Items = new ObservableCollection<object>();
      _Items.CollectionChanged += EventHandler(Changed);
    }
    return _Items;
  }
  set 
  { 
    _Items = value;
    _Items.CollectionChanged += new CollectionChangedEventHandler(Changed);
  }
}

void Changed(object sender,CollectionChangedEventArgs e)
{
  foreach(var item in e.NewValues)
  {
    if (item is Person)
      Persons.Add((Person)item);
    else if (item is Product)
      Products.Add((Product)item);
  }
}

This is just an example. But if you modify the above to meet your needs, it might get you to your goal

楠木可依 2024-09-16 03:16:01

我在此处找到了一篇博文,它让我了解了大部分内容。我使用作者 AggregateCollection 和多值转换器来完成工作。

I found a blog post here that got me most of the way. I used the authors AggregateCollection along with a multivalueconverter to get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文