Reputation: 127
I have the following ComboBox:
<ComboBox
ItemsSource="{ Binding RclTypes }"
DisplayMemberPath = "Key" SelectedValuePath = "Value"
SelectedItem="{ Binding RclTypeSelected, Mode=TwoWay }"/>
My associated ViewModel file:
private Dictionary<string, ProjectDto> _rclTypes;
public Dictionary<string, ProjectDto> RclTypes
{
get => _rclTypes;
set => SetProperty(ref _rclTypes, value);
}
private ProjectDto _rclTypeSelected;
public ProjectDto RclTypeSelected
{
get => _rclTypeSelected;
set => SetProperty(ref _rclTypeSelected, value);
}
public myConstructor()
{
PopulateRclTypes();
}
public void PopulateRclTypes()
{
_rclTypes = new Dictionary<string, ProjectDto>();
foreach (ProjectDto runType in Enum.GetValues(typeof(ProjectDto)))
{
string firstThreeLettersOfRunTypeName = runType.ToString().Substring(0, 3);
if (firstThreeLettersOfRunTypeName == "Rcl" && runType.ToString().Length > 3)
{
string displayRunTypeName = runType.ToString().Substring(3, runType.ToString().Length - 3);
_rclTypes.Add(displayRunTypeName, runType);
}
}
}
The ComboBox dropdown gets populated correctly, but RclTypeSelected property does not get filled with the right object and is always null. Why and how can I correct that?
Upvotes: 1
Views: 43
Reputation: 128147
SelectedValuePath
is meant to be used in conjunction with SelectedValue
, not SelectedItem
:
<ComboBox ItemsSource="{Binding RclTypes}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectedValue="{Binding RclTypeSelected}"/>
Upvotes: 1