- Home
- Forum
- Xamarin.Forms
- SfChipGroup(Type="Filter") does not triggering a VM event when a selection is made
SfChipGroup(Type="Filter") does not triggering a VM event when a selection is made
I created a simple SfChipGroup in XAML:
<sfSwitch:SfChipGroup
SelectedChipTextColor="#FF202020"
SelectedItems="{Binding SelectedItems}"
SelectedChipBackgroundColor="#FFB6B6B6"
SelectionIndicatorColor="#FF353535"
ChipPadding="2,0,0,0"
ItemsSource="{Binding FilterItems}"
Command="{Binding FilterCommand}"
Type="Filter"/>
The ItemsSource is populated OK. and strangely enough the content of the "SelectedItems" List<string> is updated in the ViewModel with each selection on the view... However the set property for "SelectedItems" is never called. Also the "FilterCommand" is never executed. So although the values selectedItems is updated OK in the VM, there is no trigger to do something about it (the set property is never called).
You can easly reproduce the bug on the official Syncfusion example: https://github.com/syncfusion/xamarin-demos/blob/master/Forms/Chips/Chips/Samples/ChipTypes/ChipTypes.xaml
Just place a breakpoint on the "SelectedItem" property on the ViewModel and then make a selection on the chips element set as type=filter. The set property "SelectedItems" will not be called but if you inspect the variable (selectedItems) it is actually being updated...
How can the VM be informed of the change without hard-coding the connection between the view and the ViewModel?
Thanks,
Eric
SIGN IN To post a reply.
3 Replies
HM
Hemalatha Marikumar
Syncfusion Team
November 21, 2019 10:14 AM UTC
Hi Eric,
Greetings from Syncfusion.
We have checked the following reported issues with SfChipGroup with Filter type,
· FilterCommand is not executed
· SelectedItems property setter is not called while dynamically updating the SelectedItems property of SfChipGroup
Query: FilterCommand is not executed
We would like to let you know that Command is only execute with the Action type ChipGroup. For the Filter type, we have a SelectionChanged event to intimate that chip is selected as per in below code snippet.
CodeSnippet[XAM]:
|
<SyncfusionButton:SfChipGroup SelectionChanged="SfChipGroup_SelectionChanged"
SelectedChipTextColor="#FF202020"
SelectedItems="{Binding SelectedItems,Mode=TwoWay}"
SelectedChipBackgroundColor="#FFB6B6B6"
SelectionIndicatorColor="#FF353535"
ChipPadding="2,0,0,0"
ItemsSource="{Binding FilterItems,Mode=TwoWay}"
Type="Filter"> |
CodeSnippet[C#]:
|
private void SfChipGroup_SelectionChanged(object sender, Syncfusion.Buttons.XForms.SfChip.SelectionChangedEventArgs e)
{
//To get the recently selected and unselected chip
} |
Output: While selecting the WIFI chip with Filter type
To know more about this, please refer the following UG
Query: SelectedItems property setter is not called while dynamically updating the SelectedItems property of SfChipGroup
We have checked the same with our sample and we would like to let you know that direct assigning of selected items to the SelectedItems property in ViewModel only makes the call on its setter function. It is a type of IList. Hence you can get the recently added and removed chips only with help of CollectionChanged event of this ViewModel’s property as like in below code snippet
CodeSnippet[C#] ViewModel
|
public class ChipViewModel : INotifyPropertyChanged
{
#region properties
……
private ObservableCollection<string> selectedItems = new ObservableCollection<string>();
……
public ObservableCollection<string> SelectedItems
{
get
{
return selectedItems;
}
set
{
selectedItems = value;
OnPropertyChanged("SelectedItems");
}
}
……
public ChipViewModel()
{
ActionCommand = new Command(HandleAction);
SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
}
private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
//get added chip - e.NewItems[e.NewStartingIndex]
}
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
//get removed chip - e.OldItems[e.OldStartingIndex]
}
}
……
} |
Please let us know if you have any other concern.
Regards,
Hemalatha M.
ER
Eric
November 22, 2019 04:13 PM UTC
Thank you for such a complete response Hemalatha!
This worked perfectly:
SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
Regards,
Eric
HM
Hemalatha Marikumar
Syncfusion Team
November 25, 2019 06:14 AM UTC
Hi Eric,
Thanks for your update.
We glad to hear that given solution works.
Please let us know if you need any further assistance.
Regards,
Hemalatha M.
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
ER Eric
- Nov 20, 2019 10:13 PM UTC
- Nov 25, 2019 06:14 AM UTC