Using ComboBox with ViewModel Pattern does not work?Hi,<br><br>I'm trying to control which ComboBoxItem in a ComboBox is selected by setting the IsSelected property of the underlying data items that ComboBox's ItemsSource points to. In other words, I want the ComboBox to work similar to the TreeView in Josh Smith's excellent example found here: <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">Simplifying the WPF TreeView by Using the ViewModel Pattern</a><br><br>However when I try to do this, the ComboBox fails to show the item that is selected according to the underlying data! But strangely enough, when I drop down the ComboBox, the correct item <i>is</i> pre-selected. So now I wonder why the ComboBox won't show the selected item until the drop down is expanded, and if there is any way to make the ComboBox display the selected item before this?<br><br>I have included an example below. In the example the ComboBox is supposed to display a number of items of the Person class. The Person class has an IsSelected property which is supposed to control which Person is selected in the ComboBox. Therefore the ComboBox.ItemsContainerStyle binds the Person's IsSelected property to the corresponding ComboBoxItem's IsSelected property.<br><br>I would have expected the Person instance with Name=&quot;Mike&quot; to be shown as selected in the ComboBox, but it's not. And that's what I want to fix! Any suggestions are welcome.<br><br>See XAML code below:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">&lt;</font><font style="font-size:11px">Window x:</font><font style="color:red">Class</font><font style="font-size:11px">=</font><font style="color:blue">&quot;WpfApplication1.Window1&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  <font style="color:red">xmlns</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td>  xmlns:<font style="color:red">x</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  <font style="color:red">Title</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Window1&quot;</font><font style="font-size:11px"> </font><font style="color:red">Height</font><font style="font-size:11px">=</font><font style="color:blue">&quot;300&quot;</font><font style="font-size:11px"> </font><font style="color:red">Width</font><font style="font-size:11px">=</font><font style="color:blue">&quot;300&quot;</font><font style="font-size:11px"> x:</font><font style="color:red">Name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Window&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>  <font style="color:blue">&lt;</font><font style="font-size:11px">StackPanel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">&lt;</font><font style="font-size:11px">ComboBox </font></td></tr><tr><td>      <font style="color:red">ItemsSource</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{Binding Path=Persons, ElementName=Window}&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:red">DisplayMemberPath</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Name&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>    &lt;!-- Bind ComboBoxItem's IsSelected to underlying Person's IsSelected --&gt;  <font style="color:blue"><br>    &lt;</font><font style="font-size:11px">ComboBox.ItemContainerStyle</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">&lt;</font><font style="font-size:11px">Style </font><font style="color:red">TargetType</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{x:Type ComboBoxItem}&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>          <font style="color:blue">&lt;</font><font style="font-size:11px">Setter </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">             <font style="color:red">Property</font><font style="font-size:11px">=</font><font style="color:blue">&quot;IsSelected&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td>             <font style="color:red">Value</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{Binding Path=IsSelected, Mode=TwoWay}&quot;</font><font style="font-size:11px">/&gt; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">&lt;/</font><font style="font-size:11px">Style</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td>      <font style="color:blue">&lt;/</font><font style="font-size:11px">ComboBox.ItemContainerStyle</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">&lt;/</font><font style="font-size:11px">ComboBox</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td>  <font style="color:blue">&lt;/</font><font style="font-size:11px">StackPanel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:blue">&lt;/</font><font style="font-size:11px">Window</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td> </td></tr></tbody></table></div><br>And the code behind:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Windows; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:blue">using</font><font style="font-size:11px"> System.Collections.ObjectModel; </font></td></tr><tr><td><font style="color:blue">using</font><font style="font-size:11px"> System.ComponentModel; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td><font style="color:blue">namespace</font><font style="font-size:11px"> WpfApplication1 </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">{ </td></tr><tr><td>  <font style="color:blue">public</font><font style="font-size:11px"> partial </font><font style="color:blue">class</font><font style="font-size:11px"> Window1 : Window </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  { </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> ObservableCollection&lt;Person&gt; Persons </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      <font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> (ObservableCollection&lt;Person&gt;)GetValue(PersonsProperty); } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">set</font><font style="font-size:11px"> { SetValue(PersonsProperty, value); } </font></td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">readonly</font><font style="font-size:11px"> DependencyProperty PersonsProperty = </font></td></tr><tr><td>      DependencyProperty.Register(<font style="color:blue">&quot;Persons&quot;</font><font style="font-size:11px">, </font><font style="color:blue">typeof</font><font style="font-size:11px">(ObservableCollection&lt;Person&gt;), </font><font style="color:blue">typeof</font><font style="font-size:11px">(Window1)); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> Window1() </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      InitializeComponent(); </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      // Create Persons. Pre-select the person named &quot;Mike&quot;.<br></td></tr><tr><td>      Persons = <font style="color:blue">new</font><font style="font-size:11px"> ObservableCollection&lt;Person&gt;()<br>           { </font><font style="color:blue">new</font><font style="font-size:11px"> Person(</font><font style="color:blue">&quot;John&quot;</font><font style="font-size:11px">, </font><font style="color:blue">false</font><font style="font-size:11px">), </font><font style="color:blue">new</font><font style="font-size:11px"> Person(</font><font style="color:blue">&quot;Mike&quot;</font><font style="font-size:11px">, </font><font style="color:blue">true</font><font style="font-size:11px">) }; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td>  } </td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>  <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Person : INotifyPropertyChanged </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  { </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">event</font><font style="font-size:11px"> PropertyChangedEventHandler PropertyChanged; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Name { </font><font style="color:blue">get</font><font style="font-size:11px">; </font><font style="color:blue">set</font><font style="font-size:11px">; } </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">private</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> isSelected; </font></td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> IsSelected </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      <font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> isSelected; } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">set</font><font style="font-size:11px"> </font></td></tr><tr><td>      { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">if</font><font style="font-size:11px"> (value != isSelected) </font></td></tr><tr><td>        { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">          isSelected = value; </td></tr><tr><td>          OnPropertyChanged(<font style="color:blue">&quot;IsSelected&quot;</font><font style="font-size:11px">); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        } </td></tr><tr><td>      } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> Person(</font><font style="color:blue">string</font><font style="font-size:11px"> name, </font><font style="color:blue">bool</font><font style="font-size:11px"> isSelected) </font></td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      Name = name; </td></tr><tr><td>      IsSelected = isSelected; </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">protected</font><font style="font-size:11px"> </font><font style="color:blue">virtual</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> OnPropertyChanged(</font><font style="color:blue">string</font><font style="font-size:11px"> propertyName) </font></td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">if</font><font style="font-size:11px"> (PropertyChanged != </font><font style="color:blue">null</font><font style="font-size:11px">) </font></td></tr><tr><td>      { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        PropertyChanged(<font style="color:blue">this</font><font style="font-size:11px">, </font><font style="color:blue">new</font><font style="font-size:11px"> PropertyChangedEventArgs(propertyName)); </font></td></tr><tr><td>      } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td>  } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">} </td></tr><tr><td> </td></tr></tbody></table></div><br>© 2009 Microsoft Corporation. All rights reserved.Wed, 10 Sep 2008 09:50:21 Z30a45233-dc01-40ca-9ead-fa816c395697http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30a45233-dc01-40ca-9ead-fa816c395697#30a45233-dc01-40ca-9ead-fa816c395697http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30a45233-dc01-40ca-9ead-fa816c395697#30a45233-dc01-40ca-9ead-fa816c395697John Fredman_http://social.msdn.microsoft.com/Profile/en-US/?user=John%20Fredman_Using ComboBox with ViewModel Pattern does not work?Hi,<br><br>I'm trying to control which ComboBoxItem in a ComboBox is selected by setting the IsSelected property of the underlying data items that ComboBox's ItemsSource points to. In other words, I want the ComboBox to work similar to the TreeView in Josh Smith's excellent example found here: <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">Simplifying the WPF TreeView by Using the ViewModel Pattern</a><br><br>However when I try to do this, the ComboBox fails to show the item that is selected according to the underlying data! But strangely enough, when I drop down the ComboBox, the correct item <i>is</i> pre-selected. So now I wonder why the ComboBox won't show the selected item until the drop down is expanded, and if there is any way to make the ComboBox display the selected item before this?<br><br>I have included an example below. In the example the ComboBox is supposed to display a number of items of the Person class. The Person class has an IsSelected property which is supposed to control which Person is selected in the ComboBox. Therefore the ComboBox.ItemsContainerStyle binds the Person's IsSelected property to the corresponding ComboBoxItem's IsSelected property.<br><br>I would have expected the Person instance with Name=&quot;Mike&quot; to be shown as selected in the ComboBox, but it's not. And that's what I want to fix! Any suggestions are welcome.<br><br>See XAML code below:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">&lt;</font><font style="font-size:11px">Window x:</font><font style="color:red">Class</font><font style="font-size:11px">=</font><font style="color:blue">&quot;WpfApplication1.Window1&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  <font style="color:red">xmlns</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td>  xmlns:<font style="color:red">x</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  <font style="color:red">Title</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Window1&quot;</font><font style="font-size:11px"> </font><font style="color:red">Height</font><font style="font-size:11px">=</font><font style="color:blue">&quot;300&quot;</font><font style="font-size:11px"> </font><font style="color:red">Width</font><font style="font-size:11px">=</font><font style="color:blue">&quot;300&quot;</font><font style="font-size:11px"> x:</font><font style="color:red">Name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Window&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>  <font style="color:blue">&lt;</font><font style="font-size:11px">StackPanel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">&lt;</font><font style="font-size:11px">ComboBox </font></td></tr><tr><td>      <font style="color:red">ItemsSource</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{Binding Path=Persons, ElementName=Window}&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:red">DisplayMemberPath</font><font style="font-size:11px">=</font><font style="color:blue">&quot;Name&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>    &lt;!-- Bind ComboBoxItem's IsSelected to underlying Person's IsSelected --&gt;  <font style="color:blue"><br>    &lt;</font><font style="font-size:11px">ComboBox.ItemContainerStyle</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">&lt;</font><font style="font-size:11px">Style </font><font style="color:red">TargetType</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{x:Type ComboBoxItem}&quot;</font><font style="font-size:11px">&gt; </font></td></tr><tr><td>          <font style="color:blue">&lt;</font><font style="font-size:11px">Setter </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">             <font style="color:red">Property</font><font style="font-size:11px">=</font><font style="color:blue">&quot;IsSelected&quot;</font><font style="font-size:11px"> </font></td></tr><tr><td>             <font style="color:red">Value</font><font style="font-size:11px">=</font><font style="color:blue">&quot;{Binding Path=IsSelected, Mode=TwoWay}&quot;</font><font style="font-size:11px">/&gt; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">&lt;/</font><font style="font-size:11px">Style</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td>      <font style="color:blue">&lt;/</font><font style="font-size:11px">ComboBox.ItemContainerStyle</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">&lt;/</font><font style="font-size:11px">ComboBox</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td>  <font style="color:blue">&lt;/</font><font style="font-size:11px">StackPanel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:blue">&lt;/</font><font style="font-size:11px">Window</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr><tr><td> </td></tr></tbody></table></div><br>And the code behind:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Windows; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:blue">using</font><font style="font-size:11px"> System.Collections.ObjectModel; </font></td></tr><tr><td><font style="color:blue">using</font><font style="font-size:11px"> System.ComponentModel; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td><font style="color:blue">namespace</font><font style="font-size:11px"> WpfApplication1 </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">{ </td></tr><tr><td>  <font style="color:blue">public</font><font style="font-size:11px"> partial </font><font style="color:blue">class</font><font style="font-size:11px"> Window1 : Window </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  { </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> ObservableCollection&lt;Person&gt; Persons </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      <font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> (ObservableCollection&lt;Person&gt;)GetValue(PersonsProperty); } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">set</font><font style="font-size:11px"> { SetValue(PersonsProperty, value); } </font></td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">static</font><font style="font-size:11px"> </font><font style="color:blue">readonly</font><font style="font-size:11px"> DependencyProperty PersonsProperty = </font></td></tr><tr><td>      DependencyProperty.Register(<font style="color:blue">&quot;Persons&quot;</font><font style="font-size:11px">, </font><font style="color:blue">typeof</font><font style="font-size:11px">(ObservableCollection&lt;Person&gt;), </font><font style="color:blue">typeof</font><font style="font-size:11px">(Window1)); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> Window1() </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      InitializeComponent(); </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      // Create Persons. Pre-select the person named &quot;Mike&quot;.<br></td></tr><tr><td>      Persons = <font style="color:blue">new</font><font style="font-size:11px"> ObservableCollection&lt;Person&gt;()<br>           { </font><font style="color:blue">new</font><font style="font-size:11px"> Person(</font><font style="color:blue">&quot;John&quot;</font><font style="font-size:11px">, </font><font style="color:blue">false</font><font style="font-size:11px">), </font><font style="color:blue">new</font><font style="font-size:11px"> Person(</font><font style="color:blue">&quot;Mike&quot;</font><font style="font-size:11px">, </font><font style="color:blue">true</font><font style="font-size:11px">) }; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td>  } </td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td>  <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> Person : INotifyPropertyChanged </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">  { </td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">event</font><font style="font-size:11px"> PropertyChangedEventHandler PropertyChanged; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Name { </font><font style="color:blue">get</font><font style="font-size:11px">; </font><font style="color:blue">set</font><font style="font-size:11px">; } </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">private</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> isSelected; </font></td></tr><tr><td>    <font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> IsSelected </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    { </td></tr><tr><td>      <font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> isSelected; } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">set</font><font style="font-size:11px"> </font></td></tr><tr><td>      { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">if</font><font style="font-size:11px"> (value != isSelected) </font></td></tr><tr><td>        { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">          isSelected = value; </td></tr><tr><td>          OnPropertyChanged(<font style="color:blue">&quot;IsSelected&quot;</font><font style="font-size:11px">); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        } </td></tr><tr><td>      } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">public</font><font style="font-size:11px"> Person(</font><font style="color:blue">string</font><font style="font-size:11px"> name, </font><font style="color:blue">bool</font><font style="font-size:11px"> isSelected) </font></td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      Name = name; </td></tr><tr><td>      IsSelected = isSelected; </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    <font style="color:blue">protected</font><font style="font-size:11px"> </font><font style="color:blue">virtual</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> OnPropertyChanged(</font><font style="color:blue">string</font><font style="font-size:11px"> propertyName) </font></td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">      <font style="color:blue">if</font><font style="font-size:11px"> (PropertyChanged != </font><font style="color:blue">null</font><font style="font-size:11px">) </font></td></tr><tr><td>      { </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        PropertyChanged(<font style="color:blue">this</font><font style="font-size:11px">, </font><font style="color:blue">new</font><font style="font-size:11px"> PropertyChangedEventArgs(propertyName)); </font></td></tr><tr><td>      } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">    } </td></tr><tr><td>  } </td></tr><tr><td style="background-color:rgb(247, 247, 247)">} </td></tr><tr><td> </td></tr></tbody></table></div><br>Thu, 04 Sep 2008 14:19:52 Z2008-09-04T14:42:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30a45233-dc01-40ca-9ead-fa816c395697#b50dec23-dc0d-4945-9078-9863ae34f4e6http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30a45233-dc01-40ca-9ead-fa816c395697#b50dec23-dc0d-4945-9078-9863ae34f4e6Marco Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marco%20ZhouUsing ComboBox with ViewModel Pattern does not work?This is expected, because the ItemContainerStyle will be applied only when the container aka ComboBoxItem is generated which happens when you open the ComboBox dropdown menu, to workaround this issue, you could try define another dependency property called SelectedPerson, and let the ComoBox's SelectedValue property bind to SelectedPerson property as follows:<br><br>&lt;StackPanel&gt;<br>  &lt;ComboBox<br>    ItemsSource=&quot;{Binding Path=Persons, ElementName=Window}&quot;<br>    SelectedValue=&quot;{Binding Path=SelectedPerson, ElementName=Window}&quot;<br>    DisplayMemberPath=&quot;Name&quot;&gt;<br>    &lt;ComboBox.ItemContainerStyle&gt;<br>      &lt;Style TargetType=&quot;{x:Type ComboBoxItem}&quot;&gt;<br>            &lt;Setter Property=&quot;IsSelected&quot; Value=&quot;{Binding Path=IsSelected, Mode=TwoWay}&quot;/&gt;<br>      &lt;/Style&gt;<br>    &lt;/ComboBox.ItemContainerStyle&gt;<br>  &lt;/ComboBox&gt;<br>&lt;/StackPanel&gt;<br><br>public partial class Window1 : Window<br>{<br>    public ObservableCollection&lt;Person&gt; Persons<br>    {<br>        get { return (ObservableCollection&lt;Person&gt;)GetValue(PersonsProperty); }<br>        set { SetValue(PersonsProperty, value); }<br>    }<br><br>    public object SelectedPerson<br>    {<br>        get { return (object)GetValue(SelectedPersonProperty); }<br>        set { SetValue(SelectedPersonProperty, value); }<br>    }<br><br>    public static readonly DependencyProperty SelectedPersonProperty =<br>        DependencyProperty.Register(&quot;SelectedPerson&quot;, typeof(object), typeof(Window1), new UIPropertyMetadata(null));<br><br><br>    public static readonly DependencyProperty PersonsProperty =<br>      DependencyProperty.Register(&quot;Persons&quot;, typeof(ObservableCollection&lt;Person&gt;), typeof(Window1));<br><br>    public Window1()<br>    {<br>        InitializeComponent();<br>        // Create Persons. Pre-select the person named &quot;Mike&quot;.<br>        Persons = new ObservableCollection&lt;Person&gt;() { new Person(&quot;John&quot;, false), new Person(&quot;Mike&quot;, true) };<br>        SelectedPerson = Persons[1];<br>    }<br>}<br><br>Hope this helps<br> Mon, 08 Sep 2008 06:25:54 Z2008-09-08T06:25:54Z