Skip to content

About

Official home for the Benday.Presentation & Benday.Presentation.Controls packages for .NET MAUI

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

127 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Benday.Presentation

MVVM view models and controls for WPF and .NET MAUI that reduce boilerplate and simplify data binding. Write the view models once, on plain .NET, and put either UI on top.

NuGet NuGet Downloads License: MIT

Written by Benjamin Day
Pluralsight Author | Microsoft MVP
https://www.benday.com
https://www.honestcheetah.com
info@benday.com
YouTube: https://www.youtube.com/@_benday

πŸ“– New book: Azure Cosmos DB for .NET Developers: From Document Thinking to Production Patterns β€” It's the book on Cosmos DB for .NET developers! Document modeling with aggregate roots, hierarchical partition keys, request unit economics, Change Feed patterns, and a complete production case study β€” all in C# and ASP.NET Core.

Packages

Package For What's in it NuGet
Benday.Presentation.Core Any .NET project The view models, interfaces and commands. No UI framework. NuGet
Benday.Presentation.Wpf WPF Field controls, value converters, styles, MessageBoxMessageManager NuGet
Benday.Presentation .NET MAUI Value converters, styles, MessageBoxMessageManager NuGet
Benday.Presentation.Controls .NET MAUI Field controls NuGet
Benday.Presentation.Core          (net10.0, net11.0)
β”œβ”€β”€ Benday.Presentation.Wpf       (net10.0-windows, net11.0-windows)
└── Benday.Presentation           (.NET MAUI 10 and 11)
    └── Benday.Presentation.Controls

Up to version 2.1 the view models lived in Benday.Presentation, which required MAUI. In 3.0 they moved to Benday.Presentation.Core. The namespace is still Benday.Presentation and the MAUI package forwards the moved types, so existing MAUI code keeps working.

Key Features

Benday.Presentation.Core

  • ViewModelField - One object per form field: Value, IsVisible, IsEnabled, IsValid and ValidationMessage
  • SelectableCollectionViewModel - Observable collections with automatic selection tracking (single or multi-select), drag-and-drop reordering via Move(), and selection interception via BeforeSetSelectedItem
  • FilterableListViewModel - A selectable collection with a Filter text property
  • MessagingViewModelBase / IMessageManager - Display message boxes and dialogs from your ViewModels
  • ExceptionHandlingRelayCommand / ExceptionHandlingAsyncRelayCommand - Commands that catch and display exceptions automatically

The controls (Benday.Presentation.Wpf and Benday.Presentation.Controls)

  • TextboxField - Text input with label and validation
  • TextEditorField - Multi-line text input
  • ComboboxField - Dropdown selection bound to SelectableCollectionViewModel
  • ListboxField - List selection bound to SelectableCollectionViewModel
  • LabelField - Display-only field with consistent styling

Both UI packages also have value converters (including VisibleIfNullValueConverter and VisibleIfNotNullValueConverter) and an IMessageManager that shows a native message box.

Suggestions, Problems, or Bugs?

Got ideas for features you'd like to see? Found a bug? Let us know by submitting an issue https://github.com/benday-inc/mvvm-presentation/issues. Want to contribute? Submit a pull request.

Installing

# WPF
dotnet add package Benday.Presentation.Wpf

# .NET MAUI
dotnet add package Benday.Presentation
dotnet add package Benday.Presentation.Controls

# a view model or test project with no UI
dotnet add package Benday.Presentation.Core

Quick Start

ViewModel (the same for WPF and MAUI)

using Benday.Presentation;

public class PersonViewModel : MessagingViewModelBase
{
    public PersonViewModel(IMessageManager messages) : base(messages)
    {
        SaveCommand = new ExceptionHandlingAsyncRelayCommand(Messages, SaveAsync);
    }

    public ViewModelField<string> FirstName { get; } = new(string.Empty);
    public ViewModelField<string> LastName { get; } = new(string.Empty);
    public SingleSelectListViewModel Country { get; } = new(new[]
    {
        new SelectableItem(true, "Canada"),
        new SelectableItem(false, "United States")
    });

    public ExceptionHandlingAsyncRelayCommand SaveCommand { get; }

    private async Task SaveAsync()
    {
        FirstName.IsValid = !string.IsNullOrEmpty(FirstName.Value);
        FirstName.ValidationMessage = FirstName.IsValid ? "" : "First name is required";

        if (FirstName.IsValid)
        {
            await Task.Delay(500); // save somewhere; an exception here is shown to the user
        }
    }
}

WPF XAML

<Window xmlns:controls="clr-namespace:Benday.Presentation.Wpf.Controls;assembly=Benday.Presentation.Wpf">

    <StackPanel>
        <controls:TextboxField LabelText="First Name" DataContext="{Binding FirstName}" />
        <controls:TextboxField LabelText="Last Name" DataContext="{Binding LastName}" />
        <controls:ComboboxField LabelText="Country" DataContext="{Binding Country}" />
        <Button Content="Save" Command="{Binding SaveCommand}" />
    </StackPanel>

</Window>

Set the window's DataContext to new PersonViewModel(new Benday.Presentation.Wpf.MessageBoxMessageManager()). The controls need no styles or converters in App.xaml.

MAUI XAML

<ContentPage xmlns:controls="clr-namespace:Benday.Presentation.Controls;assembly=Benday.Presentation.Controls">

    <VerticalStackLayout>
        <controls:TextboxField LabelText="First Name" BindingContext="{Binding FirstName}" />
        <controls:TextboxField LabelText="Last Name" BindingContext="{Binding LastName}" />
        <controls:ComboboxField LabelText="Country" BindingContext="{Binding Country}" />
        <Button Text="Save" Command="{Binding SaveCommand}" />
    </VerticalStackLayout>

</ContentPage>

Set the page's BindingContext to new PersonViewModel(new Benday.Presentation.MessageBoxMessageManager()). In MAUI, App.xaml has to merge Styles/Styles.xaml from Benday.Presentation and declare NotVisibilityValueConverterInstance; see Benday.Presentation.DemoApp/App.xaml.

The controls automatically handle labels, validation messages, visibility, and enabled state.

Documentation

Full documentation: https://benday-inc.github.io/mvvm-presentation/

Demo Applications

Two demo apps show every control over the same MainPageViewModel (in Benday.Presentation.DemoApp.Api, a plain .NET project):

  • Benday.Presentation.Wpf.DemoApp - WPF. dotnet run --project Benday.Presentation.Wpf.DemoApp
  • Benday.Presentation.DemoApp - .NET MAUI 11, across platforms:
iOS macOS Windows
iOS macOS Windows

Core Classes

Class Purpose
ViewModelBase Base class with INotifyPropertyChanged implementation and DebuggingTag
ViewModelField<T> Wraps a field value with Value, IsVisible, IsEnabled, IsValid, ValidationMessage
SelectableCollectionViewModel<T> Collection with selection tracking, OnItemSelected event, Move() for reordering, BeforeSetSelectedItem for interception
SingleSelectListViewModel Specialized collection for dropdown/combobox scenarios
MessagingViewModelBase Extends ViewModelBase with IMessageManager for dialogs
FilterableListViewModel<T> A selectable collection filtered by its Filter text
ExceptionHandlingRelayCommand Command that catches and displays exceptions; optional canExecute
ExceptionHandlingAsyncRelayCommand The async version: awaits the action, cannot be re-run while it is running

Building

dotnet build Benday.Presentation-Wpf.sln     # Core, WPF, WPF demo, all tests (no MAUI workload needed)
dotnet build Benday.Presentation-Maui.sln    # Core, MAUI packages, MAUI demo (needs the MAUI workload)
dotnet test --solution Benday.Presentation-Wpf.sln

Pull requests and pushes to master are built and tested by GitHub Actions (.github/workflows/benday-presentation.yml); a push to master also publishes any package version that is not on nuget.org yet.

License

MIT License - see LICENSE for details.

About

Official home for the Benday.Presentation & Benday.Presentation.Controls packages for .NET MAUI

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages