Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just tried this tonight:</p> <pre class="lang-cs prettyprint-override"><code>public class State { public string Code { get; set; } public string Name { get; set; } } public class MyWindowViewModel { ObservableCollection&lt;State&gt; _states = new ObservableCollection&lt;State&gt; { new State { Code = "FL", Name = "Florida" }, new State { Code = "CA", Name = "California" }, }; public ObservableCollection&lt;State&gt; States { get { return _states; } } } </code></pre> <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="WpfApplication1.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:app="clr-namespace:WpfApplication1" Title="Window1" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;app:ServiceLocator x:Key="Locator" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;ComboBox x:Name="TestCombo" SelectedIndex="0" DisplayMemberPath="Name" SelectedValuePath="Code"&gt; &lt;ComboBox.ItemsSource&gt; &lt;CompositeCollection&gt; &lt;app:State Code="" Name="Select a state..." /&gt; &lt;app:State Code="TX" Name="Texas" /&gt; &lt;CollectionContainer Collection="{Binding Source={StaticResource Locator}, Path=MyWindowViewModel.States}" /&gt; &lt;/CompositeCollection&gt; &lt;/ComboBox.ItemsSource&gt; &lt;/ComboBox&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>The key here is to create an instance of your service locator as a static resource then go through it to get to your viewmodel. The service locator can wire up to instances of the ViewModel using Unity or whatever DI you want.</p> <p>Edit:</p> <p>Actually in my silverlight app I create the service locator as a static resoure in the App.xaml and then bind my other UserControls/Windows/Pages DataContext to a ViewModel property of the service locator. It should still work the same way for the combo boxes though even if the service locator is instantiated in the App.xaml's resources. I wish there was a silverlight version of CompositeCollection that I could use. This would work great for the app I'm working on. :(</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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