How to split a string and insert it into Binding in the DataTemplate

Viewed 135

I have a DataTemplate of an acollectionView, inside I have a Label with the Text in Binding property.

<CollectionView.ItemTemplate>
             <DataTemplate>    
                  <Grid BackgroundColor="Gray" Opacity="0.8" RowSpacing="0.1">
                           <Label TextColor="White" Text="{Binding Data}"/>
                  </Grid>
             </DataTemplate>
</CollectionView.ItemTemplate>

I would need to be able to split that string into multiple strings, and I was able to find this code

public class DelimiterConverter : IValueConverter
        {
            public object Convert(Object value, Type targetType, object parameter, CultureInfo culture)
            {
                string[] values = ((string)value).Split(' ');
                int index = int.Parse((string)parameter);
                return values[index];
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return "";
            }
        }

HumorDiary[] note = JsonConvert.DeserializeObject<HumorDiary[]>(textt);
                 
DelimiterConverter conv = new DelimiterConverter();   
      
          foreach (HumorDiary hd in note)
          {
               conv.Convert(hd.Data, typeof(string), " ", CultureInfo.CurrentCulture);
          }

I don't know if I have entered everything right, but I would not know how to obtain the various strings divided into several parts, in the DataTemplate

1 Answers

It could be used like this:

Add the namespace where your converter is to you page

xmlns:converters="clr-namespace:MyApp.Converters"

Add the converter to your page's resources

<ContentPage.Resources>
    <converters:StringSplitConverter x:Key="StringSplitConverter" />
</ContentPage.Resources>

Edit the CollectionView's ItemTemplate to display all the separated strings in separate labels (using BindableLayout)

<CollectionView.ItemTemplate>
    <DataTemplate>
        <Grid BackgroundColor="Gray" Opacity="0.8" RowSpacing="0.1">
            <!-- Bind the ItemsSource to the Data using the converter -->
            <StackLayout BindableLayout.ItemsSource="{Binding Data, Converter={StaticResource StringSplitConverter}}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <!-- Display the separated word -->
                        <Label Text="{Binding .}" />
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
        </Grid>
    </DataTemplate>
</CollectionView.ItemTemplate>

This is the converter. Takes the string and splits it by spaces

public class StringSplitConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string stringValue)
        {
            return stringValue.Split(' ');
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Related