Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] c# – Xamarin Forms – DataTemplate from Nested Property within DataTemplate from Parent Object

Posted on November 11, 2022 By

Solution 1 :

Here you have considered two things,

The first one, for listview, you have to use ItemsSource property instead of BindableLayout.ItemsSource.

The second one is the BindingContext of the template will be a single item of the bindable layout source. For example, in your case CardData. So the source, your trying to binding to the ListView is not a part of CardData. So when you bind different objects inside the data template, you have bound with BindingContext keyword like BindingContext.YourProperty with source reference. Refer to the below code,

<StackLayout x_Name="baseLayout" Orientation="Horizontal" BackgroundColor="#EBEBEB" HeightRequest="130" 
                BindableLayout.ItemsSource="{Binding cardDatas}" WidthRequest="410">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <StackLayout HeightRequest="300" VerticalOptions="Start" Orientation="Vertical">
                <Frame CornerRadius="0" 
                            HorizontalOptions="Start" 
                            VerticalOptions="Start" 
                            Margin="0"
                            Padding="0"
                            WidthRequest="410"
                            HeightRequest="80"
                            BackgroundColor="Red"
                            HasShadow="true">
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding BindingContext.bUser.FirstName, Source={x:Reference baseLayout}}"/>
                        <Label Text="{Binding BindingContext.bUser.LastName, Source={x:Reference baseLayout}}"/>
                    </StackLayout>

                </Frame>
                <StackLayout Orientation="Vertical" BackgroundColor="Blue">
                    <ListView  ItemsSource="{Binding BindingContext.cardDatas, Source={x:Reference baseLayout}}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Label Text ="{Binding CardName}"/>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
            </StackLayout>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

And the model and view model class based on your codes,

public class User
{
    public int Uid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class CardData
{
    public int _id { get; set; }
    public string CardName { get; set; }
    public string CardNote { get; set; }
}

public class BindingUser
{
    public User bUser { get; set; }
    public ObservableCollection<CardData> cardDatas { get; set; }
    public BindingUser()
    {
        cardDatas = new ObservableCollection<CardData>()
        {
            new CardData()
            {
                _id = 1,
                    CardName = "Testing1",
                    CardNote = "Test data1",
            },
            new CardData()
            {
                _id = 2,
                    CardName = "Testing2",
                    CardNote = "Test data2",
            },
            new CardData()
            {
                _id = 3,
                    CardName = "Testing3",
                    CardNote = "Test data3",
            },
        };

        bUser = new User
        {
            Uid = 23432,
            FirstName = "First User",
            LastName = "Last User",
        };
    }
}

Solution 2 :

It’s better use the MVVM model if you’re binding complex data, the view model helps you get a clearer picture of the view and model, I made some change to the code and hope it helps:

READ  [FIXED] java - Retrieving Firebase Data with Conditions?
Powered by Inline Related Posts

XAML:

<StackLayout BindableLayout.ItemsSource="{Binding Data}"
                 Orientation="Horizontal" BackgroundColor="#EBEBEB">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame CornerRadius="0" 
                            HorizontalOptions="Start" 
                            VerticalOptions="Start" 
                            Margin="0"
                            Padding="0"
                            WidthRequest="410"
                            BackgroundColor="Red"
                            HasShadow="true">
                        <StackLayout Orientation="Vertical">
                            <Label Text="{Binding FirstName}"/>
                            <Label Text="{Binding LastName}"/>
                        </StackLayout>
                    </Frame>

                    <ListView  ItemsSource="{Binding cardDatas}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Label Text ="{Binding CardName}"/>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>

set BindingContext in MainPage:

public partial class MainPage : ContentPage
    {
        DemoViewModel viewModel = new DemoViewModel();
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = viewModel;
        }
    }

DemoViewModel:

class DemoViewModel
    {
        public ObservableCollection<BindingUser> Data { get; set; }

        public DemoViewModel() {
            Data = new ObservableCollection<BindingUser>
            {
                new BindingUser(1, "aa"),
                new BindingUser(2, "bb"),
                new BindingUser(3, "cc"),
                new BindingUser(4, "dd")
            };
        }

    }

Class User is deleted, CardData remains the same, also you can add it back if needed.

BindingUser:

class BindingUser
    {
        public int Uid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ObservableCollection<CardData> cardDatas { get; set; }

        public BindingUser(int uid, string name)
        {
            Uid = uid;
            FirstName = name;
            LastName = name;
            cardDatas = new ObservableCollection<CardData>()
            {
                new CardData()
                {
                    _id = 1,
                    CardName = "card 1",
                },
                new CardData()
                {
                    _id = 2,
                    CardName = "card 2",
                },
                new CardData()
                {
                    _id = 3,
                    CardName = "card 3",
                },
            };  
        }
    }

Problem :

I have 3 classes which are named Users, Cards, and BindingUser which is a class to bind them together.

public class User
{
    public int Uid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; } 
}

public class CardData
{
    public int _id { get; set; }
    public string CardName { get; set; }
    public string CardNote { get; set; }
}

public class BindingUser
{
    public User bUser { get; set; }
    public ObservableCollection<CardData> cardDatas { get; set; }
    public BindingUser()
    {
        cardDatas = new ObservableCollection<CardData>();
    }
}

I am trying to create a Horizontal stack layout, filled with frames for each user and in each frame is a list of cards belonging to that user.

READ  [FIXED] textview - Android: app:layout_constrainedHeight cuts last line of text in the text view
Powered by Inline Related Posts

I have tried doing this with listviews, stacklayouts and pretty much every other method google shows but each has the same result.

I get the frames for users, but they aren’t populated.

My xaml looks like this, I know this is wrong but I formatted it like this to show what I am trying to achieve. In this example, the FirstName and LastName are working fine but the listviews aren’t being populated.

<ContentPage.Content>
    <StackLayout Orientation="Horizontal" BackgroundColor="#EBEBEB" HeightRequest="130" BindableLayout.ItemsSource="{Binding bindingUsers}" WidthRequest="410">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout HeightRequest="300" VerticalOptions="Start" Orientation="Vertical">
                    <Frame CornerRadius="0" 
                                HorizontalOptions="Start" 
                                VerticalOptions="Start" 
                                Margin="0"
                                Padding="0"
                                WidthRequest="410"
                                HeightRequest="80"
                                BackgroundColor="Red"
                                HasShadow="true">
                        <StackLayout Orientation="Vertical">
                            <Label Text="{Binding bUser.FirstName}"/>
                            <Label Text="{Binding bUser.LastName}"/>
                        </StackLayout>

                    </Frame>
                    <StackLayout Orientation="Vertical" BackgroundColor="Blue">
                        <ListView  BindableLayout.ItemsSource="{Binding bindingUsers.cardDatas}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Label Text ="{Binding CardName}"/>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </StackLayout>

                </StackLayout>


            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ContentPage.Content>

Can someone point me in the right direction for what I am trying to achieve?

In .net forms it is such a simple task, and would take me minutes to do, but this has me beaten.

Comments

Comment posted by priya_d

How

Android Tags:c#, xamarin, xamarin.android, xamarin.forms, xamarin.ios

Post navigation

Previous Post: [FIXED] android – Build not succed in flutter
Next Post: [FIXED] kotlin – Opening the application from the results google.com, always directs to the home page of WebView Android

Related Posts

[FIXED] android – I’m storing a ViewPager’s current item in an Application’s subclass field for future reference. Is there a better solution? Android
[FIXED] Set up the Firebase reCAPTCHA verifier in Android Android
[FIXED] java – What is the timestamp in the CameraX.ImageInfo? Android
[FIXED] amazon web services – AWS API Gateway SDK, Issues with Android Android
[FIXED] Android Studio Kotlin Complier Error node.sym must not be null Android
[FIXED] java – Android – How to Scroll to Begining or end of ScrollView after Scrolling Stopped? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme