Nicoara
Nicoara

Reputation: 390

Data Template Selector not called

I have two types of users (user/stylist and stylist inherits from user) and in profile page I need to build it different so I'm using data template selector but the view is not calling the selector

Here is the view

<ContentPage.Resources>
    <DataTemplate x:Key="UserTemplate">
        <StackLayout>
            <Label Text="I'm a user"/>

        </StackLayout>
    </DataTemplate>
    <DataTemplate x:Key="StylistTemplate">
        <StackLayout BackgroundColor="DarkBlue">
            <Label Text="I'm a stylist"/>

        </StackLayout>
    </DataTemplate>

    <local:UserTypeSelector
        x:Key="personDataTemplateSelector"
        StylistTemplate="{StaticResource StylistTemplate}"
        UserTemplate="{StaticResource UserTemplate}" />     

</ContentPage.Resources>

<ContentPage.Content>
    <StackLayout
        Margin="10"
        BindableLayout.ItemTemplateSelector="{StaticResource personDataTemplateSelector}"
        BindableLayout.ItemsSource="{Binding User}" />      
</ContentPage.Content>

The selector class

IMPORTANT I debugged it and the OnSelectTemplate it's not called at all

public class UserTypeSelector : DataTemplateSelector
{
    public DataTemplate UserTemplate { get; set; }
    public DataTemplate StylistTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        //return ((User)item).IsStylist ? StylistTemplate : UserTemplate
        return StylistTemplate;          this is for testing the selector
    }
}

The code behind

public partial class ProfilePage : ContentPage
{ 
    public ProfilePage()
    {
        InitializeComponent();

        BindingContext = new ProfileViewModel();
    }
}

And the view model

private User user { get; set; }
public User User { get; set; }
public ProfileViewModel()
{
    //User = new User()
    //{
    //    IsStylist = true,     I tried to use this to test the selector but it throws 
    //};                        an error **Object must implement IConvertible**
}

Upvotes: 0

Views: 505

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

BindableLayout.ItemsSource should specifie the collection of IEnumerable items to be displayed by the layout.

So i modify your viewmodel,then it will work.

ProfileViewModel:

class ProfileViewModel
{
    public ObservableCollection<User> Users { get; set; }

    public ProfileViewModel()
    {
        Users = new ObservableCollection<User>()
        {
            new User(){ IsStylist = false},new User(){ IsStylist = false}

        };
    }
  public class User
    {
      public  bool IsStylist { get; set; }
    }
}

the xaml :

<ContentPage.Resources>
    <DataTemplate x:Key="UserTemplate">
        <StackLayout>
            <Label Text="I'm a user"/>

        </StackLayout>
    </DataTemplate>
    <DataTemplate x:Key="StylistTemplate">
        <StackLayout BackgroundColor="DarkBlue">
            <Label Text="I'm a stylist"/>

        </StackLayout>
    </DataTemplate>

    <local:UserTypeSelector
    x:Key="personDataTemplateSelector"
    StylistTemplate="{StaticResource StylistTemplate}"
    UserTemplate="{StaticResource UserTemplate}" />

</ContentPage.Resources>

<ContentPage.Content>
    <StackLayout
    Margin="10"
    BindableLayout.ItemTemplateSelector="{StaticResource personDataTemplateSelector}"
    BindableLayout.ItemsSource="{Binding Users}" />
</ContentPage.Content>

Upvotes: 2

Related Questions