Set DataTemplateSelector inside DataTemplateSelector

DeepakDYDeepakDY INMember ✭✭✭

Hello developers,

I want to set DataTemplateSelector inside DataTemplateSelector because i want to create row dynamically as per my image.

I implemented some coding and create view but in 1st view i want to insert my question continuously in 2nd view.

public class QuestionTemplateSelector : DataTemplateSelector
    {
        public DataTemplate AddView { get; set; }
        public DataTemplate FreeTextTemplate { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            DataTemplate resultTemp; 
            var itemModel = item as ViewPageModel;
            if (itemModel != null)
            {
                switch (itemModel.QuestionType)
                {
                         case Constant.QuestionView:
                         resultTemp = AddView;
                          break;
                }
            }
                return resultTemp;
        }
    }


    <DataTemplate x:Key="templateAddView">
                <ViewCell >
                    <Grid VerticalOptions="FillAndExpand"  BackgroundColor="{Binding Bg_Color}"
                          HorizontalOptions="FillAndExpand" Padding="10" IsVisible="{Binding Page_Visible}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" HorizontalOptions="FillAndExpand" HeightRequest="450"
                              VerticalOptions="FillAndExpand" BackgroundColor="#7F7FDE"/>
                        <Grid Grid.Row="1" ColumnSpacing="70">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Column="0" HorizontalOptions="StartAndExpand">
                                <Button WidthRequest="90" HorizontalOptions="Start" Margin="5" Clicked="Prev_Clicked"
                                        ClassId="{Binding ID}"/>
                            </Grid>
                            <Grid Grid.Column="1" HorizontalOptions="EndAndExpand">
                                <Button WidthRequest="90" HorizontalOptions="End" Margin="5" Clicked="Next_Clicked"
                                        ClassId="{Binding ID}"/>
                            </Grid>
                        </Grid>
                    </Grid>
                </ViewCell>
            </DataTemplate>

            <template:QuestionTemplateSelector x:Key="queTemplateSelector" AddView="{StaticResource templateAddView}"/>


 <ListView x:Name="SurveyList" CachingStrategy="RecycleElement"
                  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
                  SeparatorVisibility="None" HasUnevenRows="True" 
                  HeightRequest="450"
                  ItemTemplate="{StaticResource queTemplateSelector}">
        </ListView>


 listPhoto = new ObservableCollection<ViewPageModel>
                {
                    new ViewPageModel
                       {
                           ID = 1,
                           Page_S_No = "1",
                           Page_Visible = true,
                           Bg_Color ="#FE0202",
                           QueList = QuestionList,
                           QuestionType = "View",
                           //HeightRow = 450,
                           //QueList =_quesList1,
                       },
                    new ViewPageModel
                       {
                           ID = 2,
                           Page_S_No = "2",
                           Page_Visible = false,
                           Bg_Color ="#4D8E50",
                           QuestionType= "View"
                           //HeightRow = 0,
                           //QueList =_quesList1,
                       },
                       new ViewPageModel
                       {
                           ID = 3,
                           Page_S_No = "3",
                           Page_Visible = false,
                           Bg_Color ="#02FE11",
                           QuestionType = "View",
                           //HeightRow = 0,
                           //QueList =_quesList1,
                       },
                };

                SurveyList.ItemsSource = listPhoto;

Three Row Successfully added but how can add different type question in each row.

Sign In or Register to comment.