我遇到问题,当我尝试更新我的组合框选项的显示值时,它不会更新显示的所选选项。我保持它返回的相同值,只需更改该选项的显示值即可。有没有办法做到这一点?
一些视图模型:
public class NestedViewModel : ViewModelBase
{
#region Fields
private ToneAction selectedValue;
private Collection<KeyValuePair<string, SomeEnum>> options;
#endregion
NestedViewModel(){
var newOptions = new Collection<KeyValuePair<string, SomeEnum>>();
foreach (SomeEnum e in Enum.GetValues(typeof(SomeEnum)))
{
newOptions.Add(new KeyValuePair<string, SomeEnum>(Random.Next(), e));
}
vm.Options = newOptions
}
public SomeEnum SelectedValue
{
get { return selectedValue }
set
{
if (selectedValue == value)
{
return;
}
selectedValue = value;
RaisePropertyChanged(nameof(SelectedValue));
}
}
public Collection<KeyValuePair<string, SomeEnum>> Options
{
get { return options; }
set
{
if (options == value)
{
return;
}
options = value;
RaisePropertyChanged(nameof(Options));
}
}
#endregion
}
ViewWithComboBox.xaml
<ComboBox Grid.Column="1"
ItemsSource="{Binding Options}"
SelectedValuePath="Value"
DisplayMemberPath="Key"
SelectedValue="{Binding Path=SelectedValue}"/>
some service.cs
public class SomeService{
var vm = new NestedViewModel();
vm.SelectedValue = options.Last().Value; //updates and display key in combo box correctly
var newOptions = new Collection<KeyValuePair<string, SomeEnum>>();
foreach (SomeEnum e in Enum.GetValues(typeof(SomeEnum)))
{
newOptions.Add(new KeyValuePair<string, SomeEnum>(Random.Next(), e));
}
vm.Options = newOptions; //sets combo highlighted red and blank
vm.SelectedValue = options.First().Value; //updates and display key in combo box correctly
}