Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you're using MVVM. You can certainly bind to multiple ObservableCollections. The question really is: <strong>do you need to</strong>? You should reserve binding to ObserableCollections for cases where your ViewModel is changing and you need to keep your View updated with the changes. </p> <p>Here's an example I whipped up for you of a View bound to two ObservableCollections and one List in a ViewModel. So -- yes -- you can certainly bind to whatever you want. In this example, the two ObservableCollections will alternate updating. Does that help?</p> <p>I posted the code for this <a href="http://www.rootsilver.com/files/905392.zip" rel="nofollow noreferrer">here</a> if it helps to have the whole vs project.</p> <p>View:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock&gt;Bind to List:&lt;/TextBlock&gt; &lt;ListView ItemsSource="{Binding Path=Users}" Height="20"/&gt; &lt;TextBlock&gt;Bind to ObservableCollection1:&lt;/TextBlock&gt; &lt;ListView ItemsSource="{Binding Path=ObservableCollection1}" Height="100"/&gt; &lt;TextBlock&gt;Bind to ObservableCollection2:&lt;/TextBlock&gt; &lt;ListView ItemsSource="{Binding Path=ObservableCollection2}" Height="100"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>ViewModel (View is bound to this ViewModel)</p> <pre><code>using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Timers; using System.Windows; using System.Windows.Threading; namespace WpfApplication1 { public class Class1 { public List&lt;string&gt; Users{get;set;} public ObservableCollection&lt;string&gt; ObservableCollection1 { get; set; } public ObservableCollection&lt;string&gt; ObservableCollection2 { get; set; } public Class1() { this.Users = new List&lt;string&gt;{ "bob", "mary" }; this.ObservableCollection1 = new ObservableCollection&lt;string&gt;(); this.ObservableCollection2 = new ObservableCollection&lt;string&gt;(); int counter = 0; Timer t1 = new Timer(); t1.Enabled = true; t1.Interval = 1000; t1.Elapsed += delegate { Application.Current.Dispatcher.Invoke( DispatcherPriority.Send, new Action(delegate { if(counter % 2 == 1) this.ObservableCollection1.Add(DateTime.Now.ToString()); else this.ObservableCollection2.Add(DateTime.Now.ToString()); ++counter; })); }; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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