MAUI-CommunityToolkit-9 :👈 👉:MAUI-CommunityToolkit-11

Hour 10 โ€“ Advanced MAUI UI, Custom Controls & Bindable Properties

๐Ÿ“˜ Hour 10 โ€“ Advanced MAUI UI, Custom Controls & Bindable Properties

Welcome to Hour 10 of the MAUI MVVM learning series. Today, we explore more advanced UI concepts in MAUI, including:

  • โžก Creating custom reusable controls
  • โžก Creating bindable properties
  • โžก Using ControlTemplates
  • โžก Using DataTemplates effectively
  • โžก Custom Renderers / Handlers overview

โœจ 1. Custom Reusable UI Controls

MAUI allows you to create your own reusable UI components using ContentView. These help avoid duplicated UI logic.

๐Ÿงฉ 1.1 Creating a Custom Control

<!-- MyCardView.xaml -->
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             x:Class="MyApp.Controls.MyCardView">

    <Frame Padding="20" BackgroundColor="LightGray">
        <StackLayout>
            <Label x:Name="TitleLabel" FontSize="20" />
            <Label x:Name="SubtitleLabel" FontSize="14" />
        </StackLayout>
    </Frame>

</ContentView>

๐Ÿงฉ 1.2 Code-behind (with bindable properties)

// MyCardView.xaml.cs
public partial class MyCardView : ContentView
{
    public static readonly BindableProperty TitleProperty =
        BindableProperty.Create(nameof(Title), typeof(string),
            typeof(MyCardView), string.Empty, propertyChanged: OnTitleChanged);

    public string Title
    {
        get => (string)GetValue(TitleProperty);
        set => SetValue(TitleProperty, value);
    }

    private static void OnTitleChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (MyCardView)bindable;
        control.TitleLabel.Text = (string)newValue;
    }

    public static readonly BindableProperty SubtitleProperty =
        BindableProperty.Create(nameof(Subtitle), typeof(string),
            typeof(MyCardView), string.Empty, propertyChanged: OnSubtitleChanged);

    public string Subtitle
    {
        get => (string)GetValue(SubtitleProperty);
        set => SetValue(SubtitleProperty, value);
    }

    private static void OnSubtitleChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (MyCardView)bindable;
        control.SubtitleLabel.Text = (string)newValue;
    }
}

๐Ÿงช 1.3 Using the Custom Control in a Page

<controls:MyCardView Title="Welcome" Subtitle="Powered by MAUI" />

โœ” This component is now reusable anywhere in your app.


โœจ 2. Bindable Properties Explained

Bindable properties allow your controls to support data binding. They are the foundation of MAUI/MVVM communication.

  • โœ” Support two-way binding
  • โœ” Support change notifications
  • โœ” Used heavily in custom controls

๐Ÿงฉ 2.1 Short Bindable Property Example

public static readonly BindableProperty ValueProperty =
    BindableProperty.Create(nameof(Value), typeof(int),
        typeof(MySlider), 0);

public int Value
{
    get => (int)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

โœ” Add propertyChanged callback for advanced behavior.


โœจ 3. ControlTemplates

ControlTemplates let you define a UI layout that multiple controls can share.

๐Ÿ“„ 3.1 Define a Template in XAML

<ControlTemplate x:Key="CardTemplate">
    <Frame BackgroundColor="LightYellow" Padding="20">
        <ContentPresenter />
    </Frame>
</ControlTemplate>

๐Ÿ“„ 3.2 Apply the Template

<ContentView ControlTemplate="{StaticResource CardTemplate}">
    <Label Text="This is templated content" />
</ContentView>

โœ” Powerful for themeable and reusable structures.


โœจ 4. DataTemplates

DataTemplates define how each item in a collection is displayed.

๐Ÿงฉ 4.1 Collection Example

<CollectionView ItemsSource="{Binding Products}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Padding="10">
                <Label Text="{Binding Name}" />
                <Label Text="{Binding Price}" />
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

โœ” Ideal for lists, cards, grids, and custom layouts.


โœจ 5. Custom Handlers (Modern Replacement for Renderers)

Handlers allow platform-specific customization of MAUI controls. They are the modern alternative to Xamarin.Forms renderers.

๐Ÿ›  5.1 Example: Change Entry Border Color (Android)

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("BorderColor", (handler, view) =>
{
#if ANDROID
    handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.LightBlue);
#endif
});

โœ” Handlers are lightweight โœ” No inheritance needed โœ” Apply per control or globally


โœจ 6. Combining Concepts โ€“ Custom Rating Control

This example shows custom UI + bindable property.

โญ 6.1 Rating Control XAML

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             x:Class="MyApp.Controls.RatingControl">
    <HorizontalStackLayout x:Name="StarsLayout" />
</ContentView>

โญ 6.2 Rating Control Code

public partial class RatingControl : ContentView
{
    public static readonly BindableProperty RatingProperty =
        BindableProperty.Create(nameof(Rating), typeof(int),
            typeof(RatingControl), 0, propertyChanged: OnRatingChanged);

    public int Rating
    {
        get => (int)GetValue(RatingProperty);
        set => SetValue(RatingProperty, value);
    }

    public RatingControl()
    {
        InitializeComponent();
        BuildStars();
    }

    private static void OnRatingChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (RatingControl)bindable;
        control.BuildStars();
    }

    private void BuildStars()
    {
        StarsLayout.Children.Clear();
        for (int i = 1; i <= 5; i++)
        {
            string star = i <= Rating ? "โ˜…" : "โ˜†";
            StarsLayout.Children.Add(new Label { Text = star, FontSize = 30 });
        }
    }
}

โญ 6.3 Using the Rating Control

<controls:RatingControl Rating="{Binding ProductRating}" />

โœ” Clean, reusable, MVVM-friendly component.


๐Ÿ“˜ Hour 10 Summary

  • โœ” Custom controls improve reusability
  • โœ” Bindable properties enable MVVM-friendly components
  • โœ” ControlTemplates allow themed layouts
  • โœ” DataTemplates define list rendering
  • โœ” Handlers offer platform customizations
  • โœ” Complex UI components can be built easily
Back to Index
MAUI-CommunityToolkit-9 :👈 👉:MAUI-CommunityToolkit-11
*