1

Can anyone tell me why this doesn't work. I am targetting Windows at the moment.. when I run it the cells appear on the page but there is no text in them. I get the same problem with CollectionView. I have searched online for days. i have tried lots of suggestion online including different dot net versions I think this should be easy, but its got me beat!

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="testing6.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
            <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a race car number eight" />
            <ListView x:Name="EEE"
                      Grid.Row="1"
              SeparatorColor="Black"
              SeparatorVisibility="Default"
              ItemsSource="{Binding fruits}"
                      >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout
                                Margin="5,5"
                                Orientation="Vertical">
                                <Border Margin="0" Padding="8" StrokeThickness="5" Stroke="GreenYellow">
                                    <Label Text="{Binding fruits}"/>
                                </Border>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>
using System.Collections.ObjectModel;
namespace testing6
{
    public partial class MainPage : ContentPage
    {
        public class Fruit
        {
            public string? FruitName { get; set; }
        }
        public ObservableCollection<Fruit> fruits = new ObservableCollection<Fruit>();
        public ObservableCollection<Fruit> Fruits { get { return fruits; } }
        public MainPage()
        {
            InitializeComponent();
            fruits.Add(new Fruit() { FruitName = "Apple" });
            fruits.Add(new Fruit() { FruitName = "Orange" });
            fruits.Add(new Fruit() { FruitName = "Banana" });
            fruits.Add(new Fruit() { FruitName = "Grape" });
            fruits.Add(new Fruit() { FruitName = "Mango" });
            EEE.ItemsSource = fruits;
        }
    }
}
3
  • Do the correct numb er of cell appear in the Grid? If no cells appear then usually the issue is the view is not getting refreshed. If cells appear than the issue is a color issue. Saying NOT WORKING doesn't help. Commented Sep 6 at 19:35
  • Yes I get the correct number of cells., But there is no text in them. Commented Sep 6 at 22:04
  • I have also changed the background color to red, and still cant see the text. Commented Sep 6 at 22:10

1 Answer 1

2

You are binding to the wrong property. Try this instead

<Label Text="{Binding FruitName}"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou.\n<Label Text="{Binding FruitName}"/> was the perfect answer\n I couldn't see the wood for the trees.\n thankyou again.
And you should also keep in mind: Binding works ONLY with properties. Your fix with EEE.ItemsSource = fruits; can be removed when you use ItemsSource="{Binding Fruits}" in the xaml code (see the little difference in your xaml code between fruits (is a field) and Fruits (is a property))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.