vain0x / DotNetKit.Wpf.AutoCompleteComboBox

ComboBox with filtering (auto-complete) for WPF
MIT License
71 stars 26 forks source link

Existing value not applied when AutoCompleteComboBox is inside `DataGridTemplateColumn` #30

Open programatix opened 2 weeks ago

programatix commented 2 weeks ago

When added inside a DataGridTemplateColumn, the existing SelectedValue is not applied to the combobox. In the example at https://github.com/programatix/AutoCompleteComboBox.bug, each rows' PersonId is set to 10, but the combobox in the DataGrid is empty. Setting the PersonId cell also would not update the combobox. However, updating the combobox will update the PersonId cell.

vain0x commented 2 weeks ago

@programatix Thank you for a report and a reproduction. I confirmed the issue.

There is a workaround: Bind the source collection to ItemsControl.ItemsSource in addition to the ItemsSource like this:

<dotNetKitControls:AutoCompleteComboBox
    ItemsControl.ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Items}"
    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Items}"
    ...(same)>

Note that AutoCompleteComboBox.ItemsSource is defined as a different property than ItemsControl.ItemsSource.


debugging notes

<dotNetKitControls:AutoCompleteComboBox
    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Items}"
    SelectedValue="{Binding PersonId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnTargetUpdated=True}"
    Binding.TargetUpdated="AutoCompleteComboBox_TargetUpdated" ...>
    private void AutoCompleteComboBox_TargetUpdated(object sender, DataTransferEventArgs e)
    {
        Debug.WriteLine($"TargetUpdated {sender}");
    }

And then restarted the app, some logs were written to Output:

TargetUpdated DotNetKit.Windows.Controls.AutoCompleteComboBox Items.Count:0

One hypothesis is that it may be a timing issue that ComboBox's initilization happens before the AutoCompleteComboBox.ItemsSource is propagated to ItemsControl.ItemsSource.