This sounds like a mouthful but I have a BindableLayout.ItemTemplateSelector that is located inside of a DataTemplate. The problem is I can not pass a property of the DataTemplate's ItemSource for some reason.
In section 'Data Template', of 'The Code' below, I am trying to access properties inside of ItemSource="{Binding CarouselItems}" from within <controls:ThoughtEntryDataTemplateSelector>. However, I can't access anything inside of there except for StaticResource's.
I'd like to pass a property from CarouselItems into it
The Code
Data Template
<cards:CarouselView
x:Name="ThoughtCarouselViewer"
IndicatorView="indicatorView"
IsCyclical="False"
ItemsSource="{Binding CarouselItems}">
<cards:CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label
x:Name="Title"
FontAttributes="Bold"
Text="{Binding Header}"
TextColor="{StaticResource TextColor}" />
<rv:MaterialFrame
Padding="5"
LightThemeBackgroundColor="#F1F1F1"
Style="{StaticResource CardView}">
<StackLayout BindableLayout.ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type local:ThoughtRecordViewModel}}, Path=ThoughtEntryContainers}" Orientation="Vertical">
<BindableLayout.ItemTemplateSelector>
<controls:ThoughtEntryDataTemplateSelector
ChallengingThought="{StaticResource ChallengingThought}"
Distortions="{StaticResource Distortions}"
NegativeThought="{StaticResource NegativeThought}"
ThoughtType="{Binding ThoughtEntryType}" /> <============================== What I am trying to access
</BindableLayout.ItemTemplateSelector>
</StackLayout>
</rv:MaterialFrame>
</StackLayout>
</DataTemplate>
</cards:CarouselView.ItemTemplate>
</cards:CarouselView>
DataTemplateSelector
public class ThoughtEntryDataTemplateSelector : DataTemplateSelector
{
public DataTemplate NegativeThought { get; set; }
public DataTemplate Distortions { get; set; }
public DataTemplate ChallengingThought { get; set; }
public ThoughtEntryType ThoughtType { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
DataTemplate dataTemplate;
if(ThoughtType == ThoughtEntryType.NegativeThought)
{
dataTemplate = NegativeThought;
} else if(ThoughtType == ThoughtEntryType.Distortion)
{
dataTemplate = Distortions;
} else
{
dataTemplate = ChallengingThought;
}
return dataTemplate;
}
}
Carousel Items
public class ThoughtEntryCarouselItems
{
private string header;
public string Header
{
get => header;
set => header = value;
}
private ThoughtEntryType thoughtEntryType;
public ThoughtEntryType ThoughtEntryType
{
get => thoughtEntryType;
set => thoughtEntryType = value;
}
}
According to Creating a Xamarin.Forms DataTemplateSelector, I suggest you can choose a DataTemplate for CarouselView, not for StackLayout, then you can get current item ThoughtEntryType in
OnSelectTemplatemethod, please see my code.