Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding 2 ComboBox to a Dictionary, then binding the ComboBoxes to each other
    text
    copied!<p>I have a Dictionary of int to char (decimal &amp; ASCII character associated with that int). I want to have two editable combo boxes that are pre-populated with the initial values. If the user selected a value from ComboBox “A” (the dict key) I want the dict value to be populated in ComboBox “B” – and vice versa.</p> <p>It’s relatively easy to pre-populate the initial values into ComboBoxes “A” &amp; “B”. It’s the two-way binding that’s stumped me.</p> <p>Here is the VM where I populate the Dictionary:</p> <pre><code> private void InitializeSpearatorsDictionaries() { // comma, semicolon, vertical pipe, tilda int[] fields = { 44, 59, 124, 126 }; foreach (int f in fields) { FieldDict.Add(f, Convert.ToChar(f)); } } public IDictionary&lt;int, char&gt; FieldDict { get { if (_fieldDict == null) { _fieldDict = new Dictionary&lt;int, char&gt;(); } return _fieldDict; } } </code></pre> <p>Here is the initial XAML where I bind to the Dictionary (still, no problems)</p> <pre><code>&lt;StackPanel&gt; &lt;ComboBox x:Name="cbFieldChar" ItemsSource="{Binding Path=FieldDict}" SelectedIndex="0" DisplayMemberPath="Key" SelectedValuePath="Value" IsEditable="True" /&gt; &lt;ComboBox x:Name="cbFieldDecimal" ItemsSource="{Binding Path=FieldDict}" SelectedIndex="0" DisplayMemberPath="Value" SelectedValuePath="Key" IsEditable="True" /&gt; &lt;/StackPanel&gt; </code></pre> <p>Initially, i had the ItemsSource = {Binding Path=FIeldDict.Keys} and {Binding Path=FieldDict.Values}, in which case I didn't need the DisplayMemberPath and SelectedValuePath attributes, but with trying to get two-way working, I reworked it (both approaches work with the initial loading of the dictionary).</p> <p>Here's the latest attempt at getting two-way between the two ComboBoxes working</p> <pre><code>&lt;StackPanel&gt; &lt;ComboBox x:Name="cbFieldChar" ItemsSource="{Binding Path=FieldDict}" SelectedIndex="0" DisplayMemberPath="Key" SelectedValuePath="Value" IsEditable="True" /&gt; &lt;ComboBox x:Name="cbFieldDecimal" ItemsSource="{Binding Path=FieldDict}" SelectedIndex="0" DisplayMemberPath="Value" SelectedValuePath="Key" IsEditable="True" SelectedValue="{Binding ElementName=cbFieldChar, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Path=ItemsSource.Value}" /&gt; &lt;/StackPanel&gt; </code></pre> <p>Any ideas?<br> Thanks in advance,<br> --Ed</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