Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

how to bind a list to a label?

ImKhanImKhan Member ✭✭

Dear All,
i am trying to show a list, how can it be done?

this is how i get players
ListOfPlayers = ListOfPlayers ?? new ObservableCollection(); (this variable already has the list of players)

now what i did is simply binded "ListOfPlayers " with label's text property as below.

XMAL

i still cant see the list of players in the label
please let me know how to solve this
Thanks

Best Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    We can never bind a list to a Label , you could bind the list to ListView , CollectionView, or even Bindable Layouts .

    If you just show all the contents in the list (e.g the name of the players) , you could retrieve the list and combine the names into a String ,then bind it to the Label .


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • JohnHardmanJohnHardman GBUniversity admin
    edited January 7 Accepted Answer

    @ColeX said:
    We can never bind a list to a Label

    Actually, you can do this. I use it for combining multiple validation messages into one string for display using a Label.

    Here is an example. It makes use of one of my base classes (BaseConverterOneWay), but it's easily adapted to not do so. Just add an empty ConvertBack method and remove the ThrowForXXX calls.

    @ImKhan - you should be able to adapt this to use your list, rather than the list of strings that it currently uses.

        public sealed class ListOfStringsToStringConverter : 
            BaseConverterOneWay, 
            IValueConverter
        {
            public object Convert(
                object value,
                Type targetType,
                object parameter,
                CultureInfo culture)
            {
                ThrowForConvertIfUnexpectedTargetType(typeof(string), targetType);
    
                if (value is null)
                    return string.Empty;
    
                ThrowForConvertIfUnexpectedValueType(typeof(IList<string>), value);
    
                IList<string> list = (IList<string>) value;
    
                StringBuilder sb = new StringBuilder();
                bool firstTime = true;
                foreach (string listItem in list)
                {
                    if (firstTime)
                        firstTime = false;
                    else
                        sb.AppendLine();
                    sb.Append(listItem);
                }
    
                return sb.ToString();
            }
        }
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @ImKhan said:
    Dear All,
    i am trying to show a list, how can it be done?

    this is how i get players
    ListOfPlayers = ListOfPlayers ?? new ObservableCollection(); (this variable already has the list of players)

    now what i did is simply binded "ListOfPlayers " with label's text property as below.

    XMAL

    i still cant see the list of players in the label
    please let me know how to solve this
    Thanks

    Label need a string object, not a List of objects. You can use a IValueConverter to convert the ObservableCollection to a string, or use a CollectionView instead of a Label

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    We can never bind a list to a Label , you could bind the list to ListView , CollectionView, or even Bindable Layouts .

    If you just show all the contents in the list (e.g the name of the players) , you could retrieve the list and combine the names into a String ,then bind it to the Label .


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • ImKhanImKhan Member ✭✭
    Can you please explain how to retrieve list and combine names to label
  • JohnHardmanJohnHardman GBUniversity admin
    edited January 7 Accepted Answer

    @ColeX said:
    We can never bind a list to a Label

    Actually, you can do this. I use it for combining multiple validation messages into one string for display using a Label.

    Here is an example. It makes use of one of my base classes (BaseConverterOneWay), but it's easily adapted to not do so. Just add an empty ConvertBack method and remove the ThrowForXXX calls.

    @ImKhan - you should be able to adapt this to use your list, rather than the list of strings that it currently uses.

        public sealed class ListOfStringsToStringConverter : 
            BaseConverterOneWay, 
            IValueConverter
        {
            public object Convert(
                object value,
                Type targetType,
                object parameter,
                CultureInfo culture)
            {
                ThrowForConvertIfUnexpectedTargetType(typeof(string), targetType);
    
                if (value is null)
                    return string.Empty;
    
                ThrowForConvertIfUnexpectedValueType(typeof(IList<string>), value);
    
                IList<string> list = (IList<string>) value;
    
                StringBuilder sb = new StringBuilder();
                bool firstTime = true;
                foreach (string listItem in list)
                {
                    if (firstTime)
                        firstTime = false;
                    else
                        sb.AppendLine();
                    sb.Append(listItem);
                }
    
                return sb.ToString();
            }
        }
    
  • JohnHardmanJohnHardman GBUniversity admin

    @ImKhan said:
    Can you please explain how to retrieve list and combine names to label

    Use a Value Converter.

    The documentation for Value Converters can be found at https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

    Use that documentation and build something similar to the sample I posted above (I mentioned in that previous post how to adapt that code).

    Let us know how you get on.

Sign In or Register to comment.