WebMorda
WebMorda

Reputation: 101

C# markup Maui Can't bind properties

I can't figure out how to create a custom ItemTemplate for a Shell object

AppShell.xaml.cs

using AppStockAndOrdersMAUI.Views.Orders;
using CommunityToolkit.Maui.Markup;

namespace AppStockAndOrdersMAUI;

public partial class AppShell : Shell
{
    public AppShell()
    {        
        Items.Add(new ShellContent()
        {
            Title = "Home",
            ContentTemplate = new DataTemplate(() => new MainPage()),
            Route = nameof(MainPage)
        });
        
        Items.Add(new ShellContent()
        {
            Title = "Orders",
            ContentTemplate = new DataTemplate(() => new OrdersListPage()),
            Route = nameof(OrdersListPage)
        });

        ItemTemplate = new DataTemplate(() => new Label().Bind(Label.TextProperty, "Title"));
        
        Routing.RegisterRoute(nameof(OrdersListPage), typeof(OrdersListPage));
    }
}

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="AppStockAndOrdersMAUI.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:AppStockAndOrdersMAUI"
    Title="AppStockAndOrdersMAUI">
        
    
</Shell>

I've also tried

ItemTemplate = new DataTemplate(() => new Label().Bind(Label.TextProperty, nameof(ShellContent.Title)));

Items are no longer displayed in the menu

empty menu

Upvotes: 0

Views: 114

Answers (1)

WebMorda
WebMorda

Reputation: 101

Got the problem solved, added Grid, now everything displays as it should

using AppStockAndOrdersMAUI.Views.Orders;
using CommunityToolkit.Maui.Markup;

namespace AppStockAndOrdersMAUI;

public partial class AppShell : Shell
{
    public AppShell()
    {        
        Items.Add(new ShellContent()
        {
            Title = "Home",
            ContentTemplate = new DataTemplate(() => new MainPage()),
            Route = nameof(MainPage)
        });
        
        Items.Add(new ShellContent()
        {
            Title = "Orders",
            ContentTemplate = new DataTemplate(() => new OrdersListPage()),
            Route = nameof(OrdersListPage)
        });

        ItemTemplate = new DataTemplate(() => 
        
            new Grid
            {
                Padding = 10,
                Children =
                {
                    new Label().Bind(Label.TextProperty, "Title").FontSize(20)
                        .TextColor(Color.FromHex("#00A1B1"))
                        .CenterVertical()
                        .Start()
                }
            });
        
        
        Routing.RegisterRoute(nameof(OrdersListPage), typeof(OrdersListPage));
    }

Upvotes: 0

Related Questions