Preset several chips (Chiptype "Filter")
Hi,
i have a list of several sfchips in a sfchipgroup. How can I preset some of the sfchips programmatically ?. I found several similar samples, but none of them seems to work.
SelectedFilterItems.Clear();
SelectedFilterItems.Add(new ChipItem("Name1", "Adresse_Name1", 1));
SelectedFilterItems.Add(new ChipItem("Name2", "Adresse_Name2", 1));
SelectedFilterItems.Add(new ChipItem("Adresse1", "Adresse_Adresse1", 1));
SelectedFilterItems.Add(new ChipItem("Adresse2", "Adresse_Adresse2", 1));
does not set anything, but cleares the list of selected items.
<syncfusion:SfChipGroup ItemsSource="{Binding ColumnFilterChoice}" SelectedItem="{Binding SelectedFilterItems, Mode=TwoWay}" DisplayMemberPath="Name" ChipType="Filter" Grid.Row="1">
Regards
Hi Thomas Schoessow,
Thank you for reaching out to us. We have investigated your query based on your
update. We have found that you are adding SelectedFilterItems with new
instances, which are not related to the ItemsSource of SfChipGroup. This is why
the items are not being selected.
To resolve this, we suggest that when using SelectedItem, the selected items
need to add the same instance from the ItemsSource to be recognized correctly
in SfChipGroup. We have drafted a sample with the ViewModel containing the
SelectedFilterItems property and a Button with a Command to update the
SelectedItem. Please refer to the updated sample and code snippets below for
more details:
|
// XAML <syncfusion:SfChipGroup x:Name="chip" ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedFilterItems, Mode=TwoWay}" DisplayMemberPath="Name" ChipType="Filter" > </syncfusion:SfChipGroup>
<Button Text="Click to add Filter Items"
Command="{Binding AddFilterItemsCommand}"
WidthRequest="200"/> // view public class Person { public string? Name { get; set; } }
//View model class for chips public class ViewModel : INotifyPropertyChanged { private ObservableCollection<Person> employees; private ObservableCollection<Person> selectedFilterItems;
public ICommand AddFilterItemsCommand { get; private set; }
public ObservableCollection<Person> Employees { get { return employees; } set { Employees = value; OnPropertyChanged("Employees"); } }
public ObservableCollection<Person> SelectedFilterItems { get { return selectedFilterItems; } set { selectedFilterItems = value; OnPropertyChanged("Employees"); } }
public ViewModel() { employees = new ObservableCollection<Person>(); employees.Add(new Person() { Name = "John" }); employees.Add(new Person() { Name = "James" }); employees.Add(new Person() { Name = "Linda" }); employees.Add(new Person() { Name = "Rose" }); employees.Add(new Person() { Name = "Mark" });
// Add SelectedItem selectedFilterItems = new ObservableCollection<Person>(); selectedFilterItems.Add(employees[0]); selectedFilterItems.Add(employees[1]);
// Icommand to Update SelectedItems using Button Click AddFilterItemsCommand = new Command(AddFilterItems);
}
private void AddFilterItems() { //Update SelectedItems SelectedFilterItems.Clear(); SelectedFilterItems.Add(employees[3]); SelectedFilterItems.Add(employees[4]); }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } } |
Please let us know whether the provided suggestion helps to resolve your query. Don’t hesitate to contact us if you have any concerns or queries.
Regards,
Hariharan C.
Attachment: ChipMaui_da887c7d.zip
Hi Hariharan Chokkalingam,
thanks for your help, that is working now, and it seems now logical not creating new elements for the selectedItems -)
I changed your solution to:
[RelayCommand]
void Load()
{
SelectedFilterItems.Clear();
AddToSelectedItems("Rose");
AddToSelectedItems("Marc");
}
private void AddToSelectedItems(string name)
{
foreach(ChipItem chipItem in employees)
{
if(chipItem.Name == name)
{
SelectedFilterItems.Add(chipItem);
}
}
}
"My" chipitems are holding a few more information, thats why I created a small method.
Thanks
Hi Thomas,
You're welcome.
We are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.
Regards,
Preethi R
- 3 Replies
- 3 Participants
-
TS Thomas Schoessow
- Jul 9, 2024 01:10 PM UTC
- Jul 12, 2024 06:07 AM UTC