HI everybody! I have a sflist control with two group headers, as shown on the attached picture.
I have an Observable collection, which I use as the datasource of the list.
This is my header template on Xaml:
<sflistview:SfListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
BackgroundColor="{Binding Level,Converter={StaticResource TemplateConverter}}"
Padding="{Binding Level,Converter={StaticResource TemplateConverter}}">
<Label
Style="{DynamicResource EncabezadoGrupoLista}"
Text="{Binding Key}"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
Margin="3,5,3,5"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</sflistview:SfListView.GroupHeaderTemplate>
This is the Group descriptor I use on code behind.
listaActividades.DataSource.GroupDescriptors.Add(new GroupDescriptor() { PropertyName = "MODELONAME" });
listaActividades.DataSource.GroupDescriptors.Add(new GroupDescriptor() { PropertyName = "NombReferencia"});
I need to add an image in both groups, next to the items name. MODELONAME is the data shown on orange on the attached image and "NombReferencia" is the one on light orange.
Thanks for your help!
Andres.
|
public class GroupIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var group = (parameter as StackLayout).BindingContext as GroupResult;
if (group == null) return null;
if ((bool)value)
{
if (group.Level == 1)
return ImageSource.FromResource("SfListViewSample.Images.GroupExpand.png");
else
return ImageSource.FromResource("SfListViewSample.Images.SubExpand.png");
}
else
{
if (group.Level == 1)
return ImageSource.FromResource("SfListViewSample.Images.GroupCollapse.png");
else
return ImageSource.FromResource("SfListViewSample.Images.SubCollapse.png");
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
} |
|
<syncfusion:SfListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout x:Name="groupStack" BackgroundColor="{Binding Level, Converter={StaticResource groupHeaderConverter}}"
Padding="{Binding Level,Converter={StaticResource groupHeaderConverter}}"
Orientation="Horizontal">
<Label x:Name="label" Text="{Binding Key}"
FontSize="22"
FontAttributes="Bold"
VerticalOptions="Center"
HorizontalOptions="Start"/>
<Image Source="{Binding IsExpand, Converter={StaticResource groupIconConverter}, ConverterParameter={x:Reference groupStack}}"
HeightRequest="30" WidthRequest="30"
HorizontalOptions="End" VerticalOptions="Center"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</syncfusion:SfListView.GroupHeaderTemplate> |
Hi Lakshmi! It worked perfectly. Now I have another question.
This is how my observable collection is created. As I stated on my previous message, the list is grouped by first MODELONAME and then NombEntidad.
I need to set the image for the subheader based on a value inside the observablecollection, in this case IconSource.
act.Add(new ResponsablesMisActividadesViewModel
{
NombEntidad = res.data.Actividades[i].NombEntidad,
MODELONAME = res.data.Actividades[i].MODELONAME,
IconSource = "actividades.png"});
}
Is that possible to do?
Thanks.
|
public class GroupIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var group = (parameter as StackLayout).BindingContext as GroupResult;
if (group == null) return null;
if (group.Level == 1)
{
if ((bool)value) return ImageSource.FromResource("SfListViewSample.Images.GroupExpand.png");
else return ImageSource.FromResource("SfListViewSample.Images.GroupCollapse.png");
}
else
{
return GetIconSource(group);
}
}
private string GetIconSource(GroupResult group)
{
Contacts itemData = null;
foreach (var item in group.Items)
{
itemData = (Contacts)item;
break;
}
return itemData.IconSource;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
} |
Hi Lakshmi! Thanks for the update.
Now I have another question haha. Based on your code,
How can I make it that I can use more than one data source? I'm going to use this helper on several lists with different data sources and observable collections. In this sample, its attached to a specific source, which would be Contacts.
Thanks again for your help
|
private string GetIconSource(GroupResult group)
{
Contacts itemData = null;
foreach (var item in group.Items)
{
if(item is Contacts)
{
System.Diagnostics.Debug.WriteLine("Contacts executed");
itemData = (Contacts)item;
return itemData.IconSource;
}
if (item is *ModelName*)
{
...
}
…
}
return null;
} |
Thanks a lot Lakshmi! It worked flawlessly :D