[英]SelectedItem won't bind to property
我有以下組合框:
<ComboBox
ItemsSource="{ Binding RclTypes }"
DisplayMemberPath = "Key" SelectedValuePath = "Value"
SelectedItem="{ Binding RclTypeSelected, Mode=TwoWay }"/>
我關聯的 ViewModel 文件:
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);
}
}
}
ComboBox 下拉列表被正確填充,但 RclTypeSelected 屬性沒有填充正確的對象並且始終為空。 為什么以及如何糾正?
SelectedValuePath
旨在與SelectedValue
結合使用,而不是SelectedItem
:
<ComboBox ItemsSource="{Binding RclTypes}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectedValue="{Binding RclTypeSelected}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.