ItemTemplate visual BindingContext getting copied between the Items property of the CarouselView
See original GitHub issueI 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:
- Created 5 years ago
- Comments:13 (10 by maintainers)
Top GitHub Comments
I’ll try that and tell you
ok, i think you can use it as workaround I closed this issue, but if you provide me a sample, i will reopen it)