Кроме того, иногда пользователи не могут выбрать второй вариант в поле со списком. р>
Код: Выделить всё
Код: Выделить всё
public sealed class A
{
public A
{
this.ComboBoxOptions= new ObservableCollection
{
new HelperClass{ Name = "Apples", IsEnabled = true },
new HelperClass{ Name = "Bananas", IsEnabled = true },
};
this.selectedOption= this.ComboBoxOptions[0]; //Selects first option in dropdown as default
this.foo();
}
public ObservableCollection ComboBoxOptions{ get; set; }
public HelperClass SelectedOption
{
get => this.selectedOption;
set => this.SetProperty(ref this.selectedOption, value);
}
public void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0 && e.RemovedItems is HelperClass previousOption && previousOption.Name == selectedOption.Name)
{
return;
}
if (selectedOption.IsEnabled)
{
this.SelectedDuration = selectedOption;
}
else
{
comboBox.SelectedItem = e.RemovedItems.FirstOrDefault(); // Select 1st option
}
}
Void foo()
{
//Check if option 2 needs to be grayed out.
(IsGrayedOut)
{
this.ComboBoxOptions[1].IsEnabled = false;
}
else
{
this.SelectedOption = ComboBoxOptions[0]/ ComboBoxOption[1]; // Based on logic
}
}
}
I tried handling this problem by removing code for SelectionChanged event and using
public HelperClass SelectedOption
{
get => this.selectedOption;
set => this.SetProperty(ref this.selectedOption, value, () => this.Bar());
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -time-only