Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to a single element inside a CompositeCollection
    text
    copied!<p>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:</p> <pre><code>-Local Server - Endpoint 1 - Endpoint 2 -Remote - &lt;Double-click to add a server...&gt; - Remote Server 1 - Endpoint 1 - Endpoint 2 - Remote Server 2 - Endpoint 1 - Endpoint 2 </code></pre> <p>My ViewModel looks like this:</p> <pre><code>... public Server LocalServer; public ObservableCollection&lt;Server&gt; RemoteServers; ... </code></pre> <p>So, how does one go about constructing the list in xaml with a binding to a single object <strong>and</strong> 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:</p> <pre><code>&lt;CompositeCollection&gt; &lt;SingleElement Content="{Binding LocalServer}"&gt; &lt;!-- ^^ something along the lines of a ContentPresenter --&gt; &lt;TreeViewItem Header="Remote"&gt; &lt;TreeViewItem.ItemsSource&gt; &lt;CompositeCollection&gt; &lt;TreeViewItem Header="&amp;lt;Click to add...&amp;gt;" /&gt; &lt;CollectionContainer Collection="{Binding RemoteServers}" /&gt; &lt;/CompositeCollection&gt; &lt;/TreeViewItem.ItemsSource&gt; &lt;/TreeViewItem&gt; &lt;/CompositeCollection&gt; </code></pre> <p>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 <code>HierarchicalDataTemplate</code> to display the title correctly.</p> <hr> <p><strong>Update</strong></p> <p>So for now, I've exposed a property on the view model that wraps the single element in a collection so that a <code>CollectionContainer</code> may bind to it. I would really like to hear folks' ideas on how to do this, though. It seems awfully fundamental.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload