Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>as <em>default.kramer</em> said, you need to remove the <code>RelativeSource</code> from your bindings for the <code>SelectedItem</code> and <code>SelectedValue</code> like this (notice that you should add <code>Mode=TwoWay</code> to your binding so that the change in the combobox is reflected in your model). </p> <pre><code>&lt;DataGridTemplateColumn Header="House Owner"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding Path=DataContext.Owners, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" DisplayMemberPath="Name" SelectedItem="{Binding HouseOwner, Mode=TwoWay}" SelectedValue="{Binding HouseOwner.ID}" SelectedValuePath="ID"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;/DataGridTemplateColumn&gt; </code></pre> <p>However, unlike he said, you don't have to remove the binding for the <code>SelectedValue</code>. In fact, if you remove it, it won't work (both <code>SelectedValue</code> and <code>SelectedValuePath</code> should be set here, as you've done), because that's what's allowing the binding mechanism to identify the selection from the combobox to the DataGrid's <code>HouseOwner</code> property.</p> <p><code>SelectedValue</code>/<code>SelectedValuePath</code> combination is very interesting. <code>SelectedValuePath</code> tells the databinding that the <code>ID</code> property of the <code>Owner</code> object currently selected represents its <strong>value</strong>, <code>SelectedValue</code> tells it that that value should be bound to the <code>HouseOwner.ID</code> which is the selected object on the DataGrid.</p> <p>Therefore, if you remove those binding, the only thing the databinding mechanism will know is "what object is selected", and to make the correspondence between the selected item in the ComboBox and the <code>HouseOwner</code> property on the selected item in the DataGrid, they have to be "the same object reference". Meaning that, for example, the following wouldn't work:</p> <pre><code>Owners = new ObservableCollection&lt;Owner&gt; { new Owner {ID = 1, Name = "Abdou"}, new Owner {ID = 2, Name = "Moumen"} }; Houses = new ObservableCollection&lt;House&gt; { new House {ID = 1, HouseOwner = new Owner {ID = 1, Name = "Abdou" }}, new House {ID = 2, HouseOwner = new Owner {ID = 2, Name = "Moumen"}} }; </code></pre> <p>(notice that the "HouseOwners" of the Houses collection are different (new) from the ones in the Owners collection). However, the following <em>would</em> work:</p> <pre><code>Owners = new ObservableCollection&lt;Owner&gt; { new Owner {ID = 1, Name = "Abdou"}, new Owner {ID = 2, Name = "Moumen"} }; Houses = new ObservableCollection&lt;House&gt; { new House {ID = 1, HouseOwner = Owners[0]}, new House {ID = 2, HouseOwner = Owners[1]} }; </code></pre> <p>Hope this helps :)</p> <p><strong>Update:</strong> in the second case, you can get the same result without having the references being the same by overriding <strong>Equals</strong> on the <code>Owner</code> class (naturally, since it's used to compare the objects in the first place). (thanks to @<strong>RJ Lohan</strong> for noting this in the comments below)</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.
 

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