question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MauiReactor Stacklayout inside datatemplate

See original GitHub issue

Hi, I’m using some 3rd part component that have a property called ContentTemplate that accepts a DataTemplate.

I have used this code but it goes on error:

class MainPage : Component
   {
       SfPopup poppy;
       public override VisualNode Render()
       {
           poppy = new SfPopup()
           {
               IsOpen = false,
               ContentTemplate = new MauiControls.DataTemplate(() =>
               {
                   StackLayout stack = new()
                   {
                       new Label("testo popup")
                   };

                   return stack;
               })
           };

           return new ContentPage("Pagina uno")
           {
               new ScrollView() {
                   new Label("Main page"),
                   new Button().Text("Apri popup").OnClicked(ApriPopup)
               }.VCenter().HCenter()
           };
       }

       private void ApriPopup()
       {
           poppy.Show();
       }
   }

This is the erorr: image

How can i use the MauiReactor Stacklayout inside a DataTemplate?

Thank you.

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
adospacecommented, Jun 9, 2023

ok, I see that SfPopup doesn’t provide a direct way to set the content so you have to use a TemplateHost (that is a class that allows you to create a native Maui control from a MauiReactor VisualNode):

[Scaffold(typeof(Syncfusion.Maui.Popup.SfPopup))]
public partial class SfPopup 
{
    public SfPopup Content(Func<VisualNode> render)
    {
        this.Set(Syncfusion.Maui.Popup.SfPopup.ContentTemplateProperty, 
            new MauiControls.DataTemplate(() => TemplateHost.Create(render()).NativeElement));

        return this;
    }
}

[Scaffold(typeof(Syncfusion.Maui.Core.SfView))]
public abstract class SfView { }


class MainPageState
{
    public bool IsPopupOpen { get; set; }
}

class MainPage : Component<MainPageState>
{
    public override VisualNode Render()
    {
        return new ContentPage("Pagina uno")
        {
            new VStack() {
                new Label("Main page"),
                new Button().Text("Apri popup").OnClicked(ApriPopup),

                new SfPopup()
                    .Content(()=>
                        new VStack()
                        {
                            new Label("my label")
                        })
                    .IsOpen(State.IsPopupOpen)
                    .OnClosed(()=>SetState(s => s.IsPopupOpen = false, false))

            }.VCenter().HCenter()
        };
    }

    private void ApriPopup()
    {
        SetState(s => s.IsPopupOpen = true);
    }
}
1reaction
Raystorm7commented, Aug 7, 2023

ok, I see that SfPopup doesn’t provide a direct way to set the content so you have to use a TemplateHost (that is a class that allows you to create a native Maui control from a MauiReactor VisualNode):

[Scaffold(typeof(Syncfusion.Maui.Popup.SfPopup))]
public partial class SfPopup 
{
    public SfPopup Content(Func<VisualNode> render)
    {
        this.Set(Syncfusion.Maui.Popup.SfPopup.ContentTemplateProperty, 
            new MauiControls.DataTemplate(() => TemplateHost.Create(render()).NativeElement));

        return this;
    }
}

[Scaffold(typeof(Syncfusion.Maui.Core.SfView))]
public abstract class SfView { }


class MainPageState
{
    public bool IsPopupOpen { get; set; }
}

class MainPage : Component<MainPageState>
{
    public override VisualNode Render()
    {
        return new ContentPage("Pagina uno")
        {
            new VStack() {
                new Label("Main page"),
                new Button().Text("Apri popup").OnClicked(ApriPopup),

                new SfPopup()
                    .Content(()=>
                        new VStack()
                        {
                            new Label("my label")
                        })
                    .IsOpen(State.IsPopupOpen)
                    .OnClosed(()=>SetState(s => s.IsPopupOpen = false, false))

            }.VCenter().HCenter()
        };
    }

    private void ApriPopup()
    {
        SetState(s => s.IsPopupOpen = true);
    }
}

hey

This code only works for me by adding: builder.ConfigureSyncfusionCore(); at MauiProgram.cs

if not, and exception “Handler not found for Syncfusion.Maui.Popup.SfPopup” was raised

Its that ok? Docs dont mentioned nothing about this…

Hi, yes is correct.

Check the syncfusion documentation to know more: https://help.syncfusion.com/maui/popup/getting-started

There is a a sample where the app is configured and the code builder.ConfigureSyncfusionCore(); is used 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data templates - .NET MAUI
A DataTemplate is used to specify the appearance of data, and typically uses data binding to display data. A common usage scenario for...
Read more >
Binding command inside stacklayout datatemplate not ...
Trying to fire a command inside a stacklayout with itemssource. I wonder why the NavigateToProductListViewShopTappedCommand is not getting fired ...
Read more >
Component-based UI & MVU for .NET MAUI - YouTube
Join me in this video to get started with MauiReactor ! ... NET MAUI apps without XAML 1:19 - MauiReactor GitHub Repository 2:13...
Read more >
Reusable Controls and Data Template Selectors in Xamarin ...
Stop copy and pasting around XAML all over your Xamarin.Forms and .NET MAUI apps and start reusable controls, templates, cells, ...
Read more >
docs-maui/docs/fundamentals/datatemplate.md at main
DataTemplate is used to specify the appearance of data, and typically uses data binding to display data. A common usage scenario for data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found