绑定到 CompositeCollection 中的单个元素 [英] Binding to a single element inside a CompositeCollection

查看:30
本文介绍了绑定到 CompositeCollection 中的单个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成用于在网络上浏览的服务器列表,以便生成如下所示的树视图:

I am trying to produce a list of servers for browsing on a network such that it produces a tree view which looks like this:

-Local Server
 - Endpoint 1
 - Endpoint 2
-Remote
 - <Double-click to add a server...>
 - Remote Server 1
   - Endpoint 1
   - Endpoint 2
 - Remote Server 2
   - Endpoint 1
   - Endpoint 2

我的 ViewModel 如下所示:

My ViewModel looks like this:

...
public Server LocalServer;
public ObservableCollection<Server> RemoteServers;
...

那么,如何在 xaml 中构造一个绑定到单个对象对象列表的列表?我的想法可能完全错误,但我的大脑真正想做的是这样的事情:

So, how does one go about constructing the list in xaml with a binding to a single object and a list of objects? I might be thinking about it completely the wrong way, but what my brain really wants to be able to do is something like this:

<CompositeCollection>
  <SingleElement Content="{Binding LocalServer}"> 
  <!-- ^^ something along the lines of a ContentPresenter -->
  <TreeViewItem Header="Remote">
    <TreeViewItem.ItemsSource>
      <CompositeCollection>
        <TreeViewItem Header="&lt;Click to add...&gt;" />
        <CollectionContainer Collection="{Binding RemoteServers}" />
      </CompositeCollection>
    </TreeViewItem.ItemsSource>
  </TreeViewItem>
</CompositeCollection>

我觉得一定有一个基本元素我遗漏了,这使我无法在这里指定我想要的东西.那个单品有孩子.我确实尝试过使用 ContentPresenter,但无论出于何种原因,即使它选择了 HierarchicalDataTemplate 以正确显示标题,它也无法展开.

I feel like there must be a fundamental element I'm missing which keeps me from being able to specify what I want here. That single item has children. I did try using a ContentPresenter, but for whatever reason, it was not expandable even though it picked up the HierarchicalDataTemplate to display the title correctly.

更新

所以现在,我在视图模型上公开了一个属性,该属性将单个元素包装在一个集合中,以便 CollectionContainer 可以绑定到它.不过,我真的很想听听人们对如何做到这一点的想法.这似乎非常基础.

So for now, I've exposed a property on the view model that wraps the single element in a collection so that a CollectionContainer may bind to it. I would really like to hear folks' ideas on how to do this, though. It seems awfully fundamental.

推荐答案

我发布了一个与您非常相似的关于 CompositeCollections 的问题:为什么 CompositeCollection 不可冻结?

I posted a question very similar to yours regarding CompositeCollections: Why is CompositeCollection not Freezable?

这显然是 WPF 中的一个错误,信不信由你.这是 MS 员工承认的帖子:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a

This is apparently a bug in WPF, believe it or not. Here's a post by an MS employee admitting as much: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a

CompositeCollection 不可冻结,但应该可以.这使得很难将非静态元素组合到一个集合中.这是很多事情的常见场景.例如,填充其他数据绑定对象的组合框顶部的选择一个"元素会很好,但您不能以声明方式进行.

The CompositeCollection is not freezable, but should be. This makes it difficult to combine nonstatic elements into one collection. It's a common scenario for a lot of things. For example, a "Select One" element at the top of a combobox filled with other databound objects would be nice, but you can't do it declaratively.

无论如何,我很抱歉这不是一个答案,但希望它可以帮助您了解为什么这不符合您的预期.

Anyway, I'm sorry this is not an answer, but hopefully it helps you see why this isn't working how you thought it should.

这篇关于绑定到 CompositeCollection 中的单个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆