We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Show check image in a MultiSelect SfListView

Dear sirs,

I'm trying to make something similar to your example in the documentation about SfListView but I can't find a way with SelectionMode="Multiple":

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


1 Reply

DB Dinesh Babu Yadav Syncfusion Team March 15, 2017 03:21 PM UTC

Hi Marcial, 
 
Thank you for using Syncfusion Products. 
 
We have checked the reported requirement “Need to show a check mark image for the selected items in SfListView” with the given code snippet and we would like to let you know that in order to change the appearance of selected items, temp(Example: IsSelected) property should be maintained in model class and bind to an image element in the ItemTemplate as like your code snippet. And IsSelected property of underlying data item has been updated based on the selection from SelectionChanged event of SfListView and therefore the selectedimage’s source is changed by using SelectionBoolToImageConverter converter. 
 
Can you share the below details? So, that we could able to analyze the issue better and update you with appropriate solution. 
  • Can you share where you have defined the SelectedItem property in content page and why it has been used?
  • Can you share your end requirement?
 
If your requirement is to update the “iditem” in Groups class in underlying collection when select based on SelectedItem, then we have prepared a sample to meet this requirement and you can download it from below location. 

 
Could you please check with the above sample?. If your requirement has not met, please modify the above sample and provide some more additional information about your requirement. So, that we could able to update you with appropriate solution. 

Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 
 


Loader.
Live Chat Icon For mobile
Up arrow icon