I have a combobox bound to an observable collection :
public ObservableCollection<AutomationDefinition> AvailableAutomations
{
get => availableAutomations;
set => availableAutomations = value;
}
This is then setup in the XAML as:
<ComboBox
SelectedValue="{Binding SelectedAutomation, ValidatesOnDataErrors=True}"
ItemsSource="{Binding AvailableAutomations}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The type "AutomationDefinition" has a string property "Name" which is bound to.
When using the combobox manually, this all works fine, the values are displayed and set correctly:
However, when "SelectedAutomation" is set on the view model InitialiseAsync, even though certainly a valid item in the collection, the combobox appears blank as so:
(In this case, the combobox is set to "FlowB").
I have tried using both SelectedValue and SelectedItem to reference the selected item, however neither work.
Importantly, the ItemsSource "AvailableAutomations" is not set, it is initialised and populated when the viewmodel is initialised, but before, the selectedItem is set.
In the view model, it's set as so: availableAutomations = new
ObservableCollection<AutomationDefinition>()
{
... snipped, initialise with fixed test items.
}
Then, after creating that collection, at the end of the constructor:
InitializationTask =
new NotifyTaskCompletion(InitializeAsync());
In that inititialization task:
SelectedAutomation = step.SelectedAutomation ?? null;
(I've validated in debug that the step.SelectedAutomation is valid and is in the list)
There is a binding error, on the ViewModel loading:
System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0)[0].ErrorContent; DataItem='TextBox' (Name=''); target element is 'TextBox' (Name=''); target property is 'ToolTip' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.