DataTemplate Rendering

DeanChalk.1376DeanChalk.1376 GBMember, University

Hi All

I am creating a custom control for Xamarin Forms.

One of the properties on my Control is a DataTemplate (rather like the ItemsTemplate property on ListView).

For each platform I will therefore need a 'renderer' or UI factory that converts a DataTemplate into native Controls.

This 'renderer' or UI factory must already exist in order to render existing DataTemplates, but I cannot find out which Xamarin classes are responsible.

Does anyone have any experience of this

Thanks

Dean

Posts

  • LuckyDayLuckyDay USMember ✭✭✭

    The class to get Renderers is RendererFactory. But I'm not sure you need it here. DataTemplate has a method CreateContent() that will create a new XF element corresponding to your template. That's higher level than any of the custom renderer code and probably the best way to go about it.

    As example, here is my item creation code for an ItemsView I made modelled after the MS Xaml ItemsControl. It's fairly basic but gets the job done for what I need.

    protected virtual View CreateNewItem(object context)
    {
        var view = this.ItemTemplate.CreateContent() as View;
        view.BindingContext = context;
        if (ItemTap != null)
        {
            view.GestureRecognizers.Add(new TapGestureRecognizer { Command = ItemTap, CommandParameter = context });
        }
    
        return view;
    }
    
  • DeanChalk.1376DeanChalk.1376 GBMember, University

    Ahhhhhh, thanks @JonRuskin‌ - that is exactly what I was after :smiley:

Sign In or Register to comment.