question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ItemTemplate visual BindingContext getting copied between the Items property of the CarouselView

See original GitHub issue

I use the CarouselView inside a ContentView. This CarouselView has a binding to a list of Weeks (in order to print out the weeks for a certain year). Then, I use the ItemTemplate to print each week as I want to. Each week then has to print its 7 days via a DayCell custom control (another ContentView) to which I assign the BindingContext from the XAML code. Here’s the XAML:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NomadApp.Helpers.Controls.Agenda.Agenda"
             xmlns:Carousel="clr-namespace:PanCardView;assembly=PanCardView"
             xmlns:AgendaHelpers="clr-namespace:NomadApp.Helpers.Controls.Agenda"
             x:Name="this">

    <ContentView.Content>


                <Carousel:CarouselView Grid.Row="1" 
                                       x:Name="carouselView" 
                                       Items="{Binding Source={x:Reference this}, Path=Weeks}"
                                       SwipeThresholdDistance="300" 
                                       VerticalOptions="FillAndExpand" >
                    
                    <Carousel:CarouselView.ItemTemplate>
                        <DataTemplate>

                            <Grid x:Name="daysGrid">

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>

                                <AgendaHelpers:DayCell Grid.Column="0" BindingContext="{Binding Days[0]}"/>
                                <AgendaHelpers:DayCell Grid.Column="1" BindingContext="{Binding Days[1]}"/>
                                <AgendaHelpers:DayCell Grid.Column="2" BindingContext="{Binding Days[2]}"/>
                                <AgendaHelpers:DayCell Grid.Column="3" BindingContext="{Binding Days[3]}"/>
                                <AgendaHelpers:DayCell Grid.Column="4" BindingContext="{Binding Days[4]}"/>
                                <AgendaHelpers:DayCell Grid.Column="5" BindingContext="{Binding Days[5]}"/>
                                <AgendaHelpers:DayCell Grid.Column="6" BindingContext="{Binding Days[6]}"/>
                                
                            </Grid>

                        </DataTemplate>

                    </Carousel:CarouselView.ItemTemplate>
                </Carousel:CarouselView>

    </ContentView.Content>
</ContentView>

Each DayCell has a tap gesture recognizer to detect when we tap on the DayCell and when this happens, it draws a circle on this DayCell. Problem is, the drawing is getting made for same DayCell every 4 weeks. So if I select a day, then every 4 weeks, this same day gets selected.

I’m thinking of a problem in the control on how it manages the BindingContext declaration as I do it in the XAML. I know that this is not a problem from my code or the way a manage the ‘selection’ because with this https://github.com/alexrainman/CarouselView Carousel View, I do not have that problem.

Does anyone have a clue on what I can do?

For more context, this is the code for my Weeks collection and object:

Week collection bound to the CarouselView:

        /// <summary>
        /// The weeks that the agenda holds
        /// </summary>
        public ObservableCollection<Week> Weeks { get => _weeks; set { _weeks = value; OnPropertyChanged(nameof(Weeks)); } }

Week object:

    public class Week
    {
        #region Properties

        /// <summary>
        /// This week's number in the year
        /// </summary>
        public int WeekNumber { get; set; }

        /// <summary>
        /// The 7 days a week must have
        /// </summary>
        /// <remarks>
        /// TODO: Don't assign after having done bindings to this week's days, or bindings will be lost! 
        /// </remarks>
        public List<Day> Days { get; set; }

        public DateTime FirstMondayDate
        {
            get => _firstMondayDate;
            set
            {
                FirstMondayNumber = value.Day;
                _firstMondayDate = value;
            }

        }
        /// <summary>
        /// The number  in the month of the first day in this week
        /// </summary>
        /// <remarks>
        /// TODO: Don't assign after having done bindings to this week's days, or bindings will be lost! 
        /// </remarks>
        public int FirstMondayNumber
        {
            get { return _firstMondayNumber; }
            set
            {
                _firstMondayNumber = value;
                UpdateDays();
            }
        }

        /// <summary>
        /// The number in the month of the last day in this week
        /// </summary>
        public int LastSundayNumber
        {
            get { return _lastSundayNumber; }
            set
            {
                _lastSundayNumber = value;
                UpdateDays();
            }
        }


        #endregion
    }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Juansero29commented, Jul 4, 2018

I’ll try that and tell you

0reactions
AndreiMisiukevichcommented, Jul 7, 2018

ok, i think you can use it as workaround I closed this issue, but if you provide me a sample, i will reopen it)

Read more comments on GitHub >

github_iconTop Results From Across the Web

xamarin - How can I set the BindingContext of each view in ...
I'm trying to let each view in a CarouselView have the same BindingContext object as the parent ContentPage. I've tried the following which ......
Read more >
Populate a CarouselView with data - .NET MAUI
A .NET MAUI CarouselView is populated with data by setting its ItemsSource property to any collection that implements IEnumerable.
Read more >
CarouselView doesn't scroll to the CurrentItem when set
I would expect the CarouselView to scroll to that item D, instead I can see that the CurrentItem property, get the value D,...
Read more >
Binding context in xaml. DataContext = new ClassA()
Forms, we use data-binding to connect properties on a binding context (such ... sure that each item has a different DataContext, this means...
Read more >
Linear Arrangement in Xamarin Carousel View (SfCarousel)
The Carousel items can be populated in the view in a stacked linear layout by setting the ViewMode property to Linear . The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found