0

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: enter image description here

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"). enter image description here

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.
W.H
  • 187
  • 2
  • 4
  • 15
  • AvailableAutomations was a private field at first, then you edited the question and made it a private property (with invalid syntax btw). Both won't work with data binding. – Clemens Aug 21 '23 at 11:53
  • I accidentally used the backing field when writing the original question. I've updated the above. Can this please be re-opened? – W.H Aug 21 '23 at 11:55
  • Show the relevant parts of your view model, i.e. where you set the properties in question. – Clemens Aug 21 '23 at 12:10
  • I have updated the question with this - the constructor for the VM followed by the initialisation task. – W.H Aug 21 '23 at 12:25
  • That does not help much. You have to make sure that the value of the SelectedAutomation property is an element of the AvailableAutomations collection, or at least that it compares equal to an element of that collection. As a note, when you don't set SelectedValuePath, use SelectedItem instead of SelectedValue. – Clemens Aug 21 '23 at 12:28
  • That's solved it - selectedAutomation was being initialised as a member-like copy of the list of available automations, but not an exact reference. Setting it instead as "AvailableAutomations.First(x => ... matching ID ...);" has solved it. – W.H Aug 21 '23 at 12:35

0 Answers0