Model
public class Items
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
}
public class Groups
{
[PrimaryKey, AutoIncrement]
public int IdGroup { get; set; }
public string NameGroup { get; set; }
public int IdItem { get; set; }
}
ViewModel
private Items myItem;
public Items SelectedItem
{
get { return myItem; }
set
{
myItem = value;
OnPropertyChanged("SelectedItem");
}
}
private ObservableCollection <Groups> group;
public ObservableCollection <Groups> AssignedGroup
{
get { return group; }
set
{
group = value;
OnPropertyChanged("AssignedGroup");
}
}
Xaml
<sfList:SfListView x:Name="listView" Grid.Row="1"
SelectionMode="Multiple"
SelectionGesture="Tap"
SelectionBackgroundColor="#E4E4E4"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
ItemsSource="{Binding AssignedGroup}"
BackgroundColor="Yellow"
SelectionChanged="listView_SelectionChanged"
>
<sfList:SfListView.HeaderTemplate>
<DataTemplate>
<Grid BackgroundColor="#4CA1FE" HeightRequest="45">
<Label LineBreakMode="NoWrap"
Margin="10,0,0,0" Text="Groups Assigned"
FontAttributes="Bold"
FontSize="18" TextColor="White"
HorizontalOptions="Center"
VerticalOptions="Center"/>
Grid>
DataTemplate>
sfList:SfListView.HeaderTemplate>
<sfList:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="5" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="*"/>
Grid.ColumnDefinitions>
<Image Grid.Column="0" x:Name="selectionImage"
Source="{Binding IsSelected,
Converter={StaticResource BoolToImageConverter}}"
>
<Label Grid.Column="1" Text="{Binding NameGroup}"
TextColor="Black" FontSize="20" />
Grid>
DataTemplate>
sfList:SfListView.ItemTemplate>
sfList:SfListView>
Converter
public class SelectionBoolToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
return ImageSource.FromResource("Selected.png");
else
return ImageSource.FromResource("NotSelected.png");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
In the ViewModel, the AssignedGroup (derived from Groups table) contains all the groups stating whether the SelectedItem pertains or not to each group. If the Item is included in a group, the field IdItem contains the Id value of the item, or zero in the other case:
IdGroup |
NameGroup |
IdItem |
|
1 |
Group 1 |
0 |
Item 1 is NOT included in Group 1 |
2 |
Group 2 |
1 |
Item 1 is included in Group 2 |
3 |
Group 3 |
0 |
Item 1 is NOT included in Group 3 |
I’m trying to customize a SfListView in the same way of the example shown in
https://help.syncfusion.com/xamarin/sflistview/selection
but I can’t figure how can I substitute the IsSelected with something like
AssignedGroup.IdItem == SelectedItem.Id
without Multibinding. I use the same converter than the one in the example, but nothing happens:
<Image Grid.Column="0" x:Name="selectionImage"
Source="{Binding IsSelected,
Converter={StaticResource BoolToImageConverter}}" >
Please note that SfListView has SelectionMode="Multiple". The idea is when you select an item in the SfListView, change the underlaying AssignedGroup.IdItem = SelectedItem.Id, and when you deselect one, change AssignedGroup.IdItem = 0
Thank you very much for your help
Marcial González