This is a migrated thread and some comments may be shown as answers.

Problem with binding to SelectedItems

3 Answers 100 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Michal
Top achievements
Rank 1
Michal asked on 02 Sep 2020, 04:27 AM

Hi,

I got problem with SelectedItems binding - If I put breakpoint to the Setter of the SelectedComboFilterItems it will never be hit.

View:

<telerik:RadAutoCompleteBox x:Name="ComboFilterAutoCompleteBox" Grid.Column="1" Grid.Row="5"
  Margin="1" HorizontalAlignment="Stretch"   TextSearchMode="Contains"
  ScrollViewer.VerticalScrollBarVisibility="Auto"
  TextSearchPath="FormattedName" DisplayMemberPath="Name" SelectionMode="Multiple"
  ItemsSource="{Binding ComboFilterItems,Mode=TwoWay}"
  SelectedItems="{Binding SelectedComboFilterItems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  IsEnabled="{Binding ComboFilterEnabled}"
  FilteringBehavior="{StaticResource CustomFilteringBehavior}"
   <telerik:RadAutoCompleteBox.DropDownItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FormattedName}"/>
                </DataTemplate>
    </telerik:RadAutoCompleteBox.DropDownItemTemplate>
</telerik:RadAutoCompleteBox>

 

Here is ViewModel:

 

public ObservableCollection<DocumentType> mSelectedComboFilterItems = new ObservableCollection<DocumentType>();
public ObservableCollection<DocumentType> SelectedComboFilterItems
{
    get
    {
        return mSelectedComboFilterItems;
    }
    set
    {
        mSelectedComboFilterItems = value;
    }
}
public ObservableCollection<DocumentType> ComboFilterItems { get; set; } = new ObservableCollection<DocumentType>();

 

Rest of the binding is working fine.

If I select few items the SelectedComboFilterItems will hold those values. Just for some reason the setter is never called.

Cheers

Mike

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 04 Sep 2020, 02:53 PM

Hi Michal,

The behavior you described is expected as the initial value of the collection is set through the mSelectedComboFilterItems field.

As selecting new items through the UI does not replace the collection but simply adds to it due do the TwoWay binding, the setter will not be invoked. If you want to keep track of the items being added or removed from the collection, you can handle its CollectionChanged event like so:

        private void SelectedComboFilterItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
        }
Please let me know if this is what you're aiming for.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Michal
Top achievements
Rank 1
answered on 06 Sep 2020, 03:31 PM

Hi Dilyan,

Thank you for answering my question.

Now I know why it's not working as I expected it to.

I'm already using the CollectionChanged, but wanted to do it the MVVM way to keep it clean.

Cheers

Mike

 

 

0
Dilyan Traykov
Telerik team
answered on 08 Sep 2020, 07:00 AM

Hi Mike,

I'm happy to hear you found my reply helpful.

I believe you can still follow the MVVM pattern and handle the CollectionChanged event in the viewmodel. Do let me know if you need any help with the concrete implementation of this.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
AutoCompleteBox
Asked by
Michal
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Michal
Top achievements
Rank 1
Share this question
or