Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to get this working. But I set things up a wee bit differently.</p> <ol> <li>I created a ViewModel to act as a contract with the View.</li> <li>I bound to the ComboBox.SelectedItem Property instead of ComboBox.SelectedValue Property</li> </ol> <p>Since I didn't know what your data source was I made up my own to simulate the basic problem: having a comboBox bind correctly within a WPF DataGrid.</p> <p>Here is the composition of my View Model:</p> <pre><code>public class RootViewModel { public List&lt;State&gt; USStates { get; set; } public List&lt;Customer&gt; Customers { get; set; } public ViewModel() { Customers = new List&lt;Customer&gt;(); Customers.Add(new Customer() { FirstName = "John", LastName = "Smith", State = new State() { ShortName = "IL" } }); Customers.Add(new Customer() { FirstName = "John", LastName = "Doe", State = new State() { ShortName = "OH" } }); Customers.Add(new Customer() { FirstName = "Sally", LastName = "Smith", State = new State() { ShortName = "IN" } }); USStates = new List&lt;State&gt;(); USStates.Add(new State() { ShortName = "OH" }); USStates.Add(new State() { ShortName = "IL" }); USStates.Add(new State() { ShortName = "IN" }); } } public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public State State { get; set; } } public class State { public string ShortName { get; set; } public State() { ShortName = string.Empty; } public override bool Equals(object obj) { if (obj is State) { State otherState = obj as State; return ShortName.Equals(otherState.ShortName); } else { return false; } } } </code></pre> <p>Before we begin, I set the DataContext of my Window to be an instance of my properly constructed RootViewModel.</p> <pre><code>&lt;tk:DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False"&gt; &lt;tk:DataGrid.Columns&gt; &lt;tk:DataGridTemplateColumn Header="State"&gt; &lt;tk:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=State.ShortName, Mode=TwoWay}" /&gt; &lt;/DataTemplate&gt; &lt;/tk:DataGridTemplateColumn.CellTemplate&gt; &lt;tk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox x:Name="cboDonationPurpose" SelectedItem="{Binding Path=State, Mode=TwoWay}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}, Path=DataContext.USStates}" DisplayMemberPath="ShortName" SelectedValuePath="ShortName" /&gt; &lt;/DataTemplate&gt; &lt;/tk:DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/tk:DataGridTemplateColumn&gt; &lt;/tk:DataGrid.Columns&gt; &lt;/tk:DataGrid&gt; </code></pre> <p>In order for the SelectedItem to bind properly I need to ensure that I have overriden the Equals method on my entity since under the hood, WPF is using this method to determine who is the SelectedItem or not. I think this was fundamentally your problem from the beginning which caused you to try to bind to SelectedValue instead of SelectedItem.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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