I have a ListBox:
<ListBox
ItemsSource="{Binding SupplyGroups}"
SelectedItem="{Binding ModelSupplyGroup, Mode=TwoWay}"/>
SupplyGroups
is an ObservableCollection<SupplyGroup>
SupplyGroup
is an object with an int SupplyGroupId
and a string SupplyGroupTitle
. There is a public override string ToString()
so the ListBox gets the Title for display.
ModelSupplyGroup
is a Property that accesses the Model's SupplyGroup.
When the window loads up, the value saved as ModelSupplyGroup
does not cause the matching ListBox row to be selected.
If I modify the code so SupplyGroup
object is replaced with a string
, it works correctly.
The binding appears to work in the opposite direction, so if a ListBox row is selected, it will update the bound ModelSupplyGroup
correctly.
Inspecting the Objects in the matching SupplyGroup in the ObervableCollection and ModelSupplyGroup, they have identical values for ID and Title.
public SupplyGroup ModelSupplyGroup
{
get
{
return this.purchasingRepository.GetSupplyGroupById(this.Model.SupplyGroupId);
}
set
{
this.Model.SupplyGroup = value;
}
}
The GetSupplyGroupById()
returns the full object from the Id.