If I understand what you're trying to do correctly, I think this is what you're after (based on my mockup from your description):
Note some of my variables have slightly different names due to my mockup e.g. UserControl
.
<DataGridComboBoxColumn
SelectedItemBinding="{Binding Street}"
DisplayMemberPath="StreetName">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Streets, RelativeSource={RelativeSource FindAncestor,AncestorType=UserControl}, Mode=OneWay}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Streets, RelativeSource={RelativeSource FindAncestor,AncestorType=UserControl}, Mode=OneWay}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
For me, this displays my Cars
in a datagrid, with all the relevant columns, and selectable streets (you can adapt for Houses
in a similar way).
As I think you've read, DataGridComboBoxColumn
is a bit odd about resolving its DataContext
. When you're trying to set the ItemsSource
at the top of the DataGridComboBoxColumn
, it's causing confusion.
this should work for the example you've described (which I've modelled with Cars
and Houses
having a Street
object as the tracking property, rather than an ID).
If you decide you want to incorporate SelectedValue
and use the returned Street
ID (to store as ID properties on Car
and House
, I think you'll have to do it with another setter in your style templates.
Edit: The Mockup I used (Datacontext set to self in XAML). N.B the objects won't track each other (as I wasn't sure what set up you had).
public partial class UserControl1 : UserControl
{
public List<Street> Streets { get; set; }
public List<House> Houses { get; set; }
public List<Car> Cars { get; set; }
public UserControl1()
{
Streets = new List<Street>();
Houses = new List<House>();
Cars = new List<Car>();
Street streetOne = new Street() { StreetID = 1, Name = "Street One" };
Street streetTwo = new Street() { StreetID = 1, Name = "Street Two" };
Car carOne = new Car() { CarID = 1, Name = "KITT", MyStreet = streetOne };
Car carTwo = new Car() { CarID = 2, Name = "Car 2", MyStreet = streetTwo };
House houseOne = new House() { HouseID = 1, Name = "House 1", MyStreet = streetOne };
InitializeComponent();
streetOne.MyCars.Add(carOne);
streetOne.MyHouses.Add(houseOne);
streetTwo.MyCars.Add(carTwo);
Cars.Add(carOne);
Cars.Add(carTwo);
Houses.Add(houseOne);
Streets.Add(streetOne);
Streets.Add(streetTwo);
}
}
public class Car
{
public int CarID { get; set; }
public string Name { get; set; }
private Street _street;
public Street MyStreet
{
get { return this._street; }
set { this._street = value; }
}
}
public class House
{
public int HouseID { get; set; }
public string Name { get; set; }
public Street MyStreet { get; set; }
}
public class Street
{
public int StreetID { get; set; }
public string Name { get; set; }
public List<House> MyHouses { get; set; }
public List<Car> MyCars { get; set; }
public Street()
{
MyHouses = new List<House>();
MyCars = new List<Car>();
}
}