ModernWPF Documentation

repository·master·Indexed 26 days ago

https://github.com/kinnara/modernwpf

ModernWPF provides Fluent styling and WinUI-inspired controls for Windows Presentation Foundation (WPF) applications. It offers modern UI elements like NavigationView, ContentDialog, and InfoBar, as well as a WPF implementation of WinUI 3 AppBar controls. The library runs directly on the WPF runtime and includes specialized components such as the AnnotatedScrollBar. It is available via the ModernWpfUI NuGet package.

Tokens
88.4K
Snippets
54
Records
498
Agent score
89%

What's inside ModernWPF

  1. Understand ModernWPF Fluent Backport Syncing

    master

    ModernWPF periodically synchronizes its resource values and control templates with the official Microsoft WPF Fluent theme. This ensures that ModernWPF applications maintain visual parity with the official platform while preserving backward compatibility for older target frameworks (like net462).

    Key behaviors to note:

    • Target Framework Differences: On net10.0-windows7.0, ModernWPF uses official templates via FluentControlsResources. On older frameworks (net462, net8.0-windows7.0), it uses backported templates that mimic the official look.
    • Resource Aliases: ModernWPF often uses its own resource aliases (e.g., ButtonBackground instead of official WPF Fluent keys) to maintain a stable public API surface, even when the underlying values are synced from the official source.
    • Intentional Differences: Some values (like TextControlThemeMinWidth or specific CornerRadius implementations) are intentionally kept different from the official WPF Fluent to prevent layout breaks in existing ModernWPF applications.
  2. Understand WinUI 3 Control Source Coverage

    master

    ModernWPF provides WinUI-derived controls for WPF. The WinUI 3 Control Source Coverage audit ensures that the resource dictionaries merged by ModernWpf.Controls\Themes\Generic.xaml maintain parity with the official WinUI 3 source code (microsoft/microsoft-ui-xaml).

    When using ModernWPF controls, you can refer to the specific source audit documentation to understand how a WPF control maps to its WinUI 3 counterpart and what substitutions were made.

  3. Understand ScrollBar implementation in ModernWpf

    master

    ModernWpf uses the official WPF Fluent ScrollBar style as the source for the stock System.Windows.Controls.Primitives.ScrollBar template. This ensures that the ScrollBar in ModernWpf applications behaves identically to the official WPF Fluent implementation.

    Key Behaviors

    • DefaultStyle: The DefaultScrollBarStyle follows the official WPF Fluent setter surface, including Margin=0, Padding=0, SnapsToDevicePixels=True, and OverridesDefaultStyle=True.
    • Orientation: Orientation selection uses official WPF Fluent triggers:
      • Horizontal: Sets Height=12 and uses HorizontalScrollBarTemplate.
      • Vertical: Sets Width=12 and uses VerticalScrollBarTemplate.
    • Template Parts: The templates utilize official WPF Fluent parts: PART_Border, line-button, PART_Track, page-button, and thumb shape.
    • Compatibility: For older target frameworks, System.Runtime namespace references are mapped to mscorlib, and Border.CornerRadius is mapped to maintain the 4px thumb radius via the existing ModernWpf backport bridge.
  4. Understand ResizeGrip behavior and styling in ModernWPF

    master

    ModernWPF maps the ResizeGrip control to the standard WPF System.Windows.Controls.Primitives.ResizeGrip. The styling in ModernWPF is designed to match the official WPF Fluent design language.

    Key characteristics of the ModernWPF ResizeGrip include:

    • Glyph-based Template: Uses a plain WPF TextBlock with a glyph instead of the older tiled Path or DrawingBrush drawings.
    • Property Alignment: The following properties are aligned with official WPF Fluent values:
      • ResizeGripMinHeight
      • ResizeGripMinWidth
      • ResizeGripIconSize
      • ResizeGripIconGlyph
    • Template Resources: The template utilizes SymbolThemeFontFamily, ResizeGripIconGlyph, ResizeGripIconSize, and ResizeGripForeground.
    • Theme Aliases: ResizeGripForeground is provided via theme resource aliases:
      • Light/Dark Themes: Points to ControlStrongFillColorDefaultBrush.
      • High Contrast Theme: Points to SystemColorButtonTextColorBrush.
  5. Understand WinUI 3 Source Parity in ModernWPF

    master

    ModernWPF implements 'Source Parity' for many standard WPF controls. This means instead of using custom 'guesses' or helper classes (like ContentPresenterEx, VisualStateEx, or FocusVisualHelper), ModernWPF now uses the official WPF Fluent-backed styles and templates directly from the Microsoft .NET WPF source.

    This approach ensures that stock controls like ListBox, TreeView, Menu, and Window behave exactly like official WinUI/Fluent controls while remaining compatible with standard WPF.

    Key benefits include:

    • Use of official WPF triggers and template shapes.
    • Use of official glyph resources and aliases.
    • Improved compatibility with standard WPF presenters.
    • Reduced reliance on custom internal helper classes.
  6. Understand the ModernWpf WrapPanel implementation

    master

    The ModernWpf WrapPanel is a source-backed WPF port of the official WinUI 3 implementation. It is not a legacy WPF approximation but follows the WinUI 3 row-building and layout algorithms.

    Key characteristics include:

    • Public Surface: Matches WinUI 3 IDL, including Padding, ItemSpacing, LineSpacing, Orientation, and ItemsStretch (with WrapPanelItemsStretch.None or Last).
    • Layout Behavior:
      • Measure: Children are measured with padded available size. Collapsed children are skipped without adding spacing. Row height is determined by the maximum child cross size.
      • Arrange: Implements a source cache rule where rows are refreshed only when the arranged primary-axis size is smaller than the measured desired size. This preserves row breaks when a parent expands in width.
    • Automation: Relies on the standard platform panel/child automation tree rather than a custom automation peer.
  7. Understand ListBox and ListView implementation in ModernWPF

    master

    ModernWPF has updated its ListBox, ListView, and GridView styles to match the official WPF Fluent source templates. This ensures visual parity with the official Microsoft Fluent design for WPF while maintaining compatibility with older targets.

    Key Implementation Details

    • Templates: ListBox.xaml, ListBoxItem.xaml, GridView.xaml, ListView.xaml, and ListViewItem.xaml now use official stock WPF Fluent templates.
    • Resource Management: The official split dictionaries are merged via ModernWpf\StockControlsResources.xaml to provide a single public entry point for stock controls.
    • Converters: Instead of using the official WPF Fluent.Controls.ViewIsGridViewConverter, ModernWPF uses its own ModernWpf.Controls.Primitives.IsGridViewConverter to maintain compatibility.
    • Theming: Official resource brush definitions are mapped to ModernWPF aliases in ThemeResources\Light.xaml, Dark.xaml, and HighContrast.xaml to integrate with the existing ModernWPF theme model.
  8. Understand ModernWpf Core Resource Statuses

    master

    ModernWpf resources merged via ModernWpf\Themes\Generic.xaml are categorized into four statuses that define their origin and purpose:

    • WinUI 3 source-backed WPF port: A resource belonging to a ModernWpf control mapped to local WinUI 3 source.
    • WinUI 3 source-backed WPF compatibility layer: A WPF compatibility layer for WinUI template behavior that standard WPF controls do not expose directly.
    • Official WPF Fluent shell substitution: A resource belonging to ModernWpf's custom shell around a stock WPF surface, designed for compatible content-window behavior.
    • ModernWpf compatibility resource: A repo-specific support resource retained for existing source-backed templates or compatibility integration.
  9. Understand the ModernWpf LayoutPanel implementation

    master

    The LayoutPanel in ModernWpf is a source-backed WPF port of the WinUI 3 LayoutPanel control. It provides a layout surface that follows the WinUI 3 algorithm for measuring and arranging children.

    Key Features

    • Public Surface: Supports Layout, BorderBrush, BorderThickness, Padding, and CornerRadius.
    • Layout Logic: Available/final size is reduced by padding and border thickness (clamped to zero), then delegated to the specified Layout. If no layout is present, it applies the size as a single fill rectangle to every child.
    • Layout Replacement: When the Layout property is changed, the old layout is detached, the new layout is initialized and hooked, and the panel invalidates its measure.
    • Chrome Rendering: Uses LayoutChromeHelper to render equivalent border and corner radius effects in WPF.
    • Clipping: Rounded layout clipping and hit testing are implemented using WPF Geometry and HitTestCore.
    • Children Access: Provides a lightweight IReadOnlyList<UIElement> wrapper over the UIElementCollection to mimic WinUI's Children().GetView() behavior.
  10. Understand TabControl and TabItem styling in ModernWPF

    master

    ModernWPF implements TabControl and TabItem by using the official WPF Fluent styles as the primary source. Instead of using a custom WinUI-shaped TabView implementation, ModernWPF leverages the stock WPF TabControl behavior to ensure compatibility with official WPF Fluent design patterns.

    The following official styles are maintained within ModernWPF:

    • DefaultTopTabControlStyle
    • DefaultBottomTabControlStyle
    • DefaultLeftTabControlStyle
    • DefaultRightTabControlStyle
    • DefaultTabControlStyle
    • DefaultTabItemStyle
    • Implicit TabControl and TabItem styles
  11. Understand ThemeShadow WinUI 3 Parity in ModernWPF

    master

    ModernWPF implements a WPF-based substitution for the WinUI ThemeShadow compositor. While not a literal port of the WinUI compositor, it achieves parity by deriving shadow properties—such as depth, blur, offset, inset, hollow-center behavior, and light/dark opacity constants—directly from WinUI source code and MockDComp visual-tree masters.

    Key technical differences to note:

    • Rendering Engine: ModernWPF uses a software-rendered ThemeShadowChrome substitute instead of the WinUI compositor's UIElement.Shadow visuals.
    • Corner Radius: ModernWPF uses a direct CornerRadius mask, whereas WinUI uses an adjusted NineGridBrush with insets and independent X/Y radii.
    • Opacity: ModernWPF applies child opacity to a private shadow host to match WinUI behavior.
    • Geometry: Shadow geometry is validated against WinUI MockDComp masters to ensure the WPF renderer's bitmap size and content offsets match WinUI's expected output for various scenarios (e.g., basic drop shadows, dynamic corner radii, and windowed popups).
  12. Understand ModernWPF Layout Chrome Adapters

    master

    ModernWPF provides a set of specialized WPF adapters designed to replicate WinUI 3 layout and composition behaviors within a WPF environment. These adapters ensure that layout properties like spacing, rounded corners, and content transitions behave identically to WinUI 3.

    Key adapters include:

    • BorderEx: Handles rounded corner painting and background sizing (outer vs. inner edge).
    • GridEx: Adds RowSpacing and ColumnSpacing support to the standard WPF Grid, including support for negative spacing and multi-track spans.
    • StackPanelEx: Implements WinUI-style spacing (only between visible children) and exposes snap-point contracts for scrolling.
    • ContentPresenterEx: The preferred surface for text and chrome. It forwards properties like CharacterSpacing, IsTextScaleFactorEnabled, TextWrapping, LineHeight, and MaxLines to the underlying text element.
    • ContentControlEx: Manages content transitions, specifically mapping NavigationThemeTransition to the WPF NavigationAnimation engine.