Skip to content

AspectRatio

AspectRatio (NaviusAspectRatio) is a ContentControl overriding MeasureOverride to enforce a fixed width:height ratio on its single child. It is a pure layout primitive: there is no CSS cascade to replicate in WPF, so the child fills the computed box directly via the default ContentPresenter rather than needing a separate inner element.

xmlns:navius="clr-namespace:Navius.Wpf.Primitives.Controls;assembly=Navius.Wpf.Primitives"
<navius:NaviusAspectRatio Ratio="1.7777">
<Image Source="banner.png" Stretch="UniformToFill" />
</navius:NaviusAspectRatio>
Property Type Default From Description
Ratio double 1.0 NaviusAspectRatio Desired width:height ratio.
EffectiveRatio double computed NaviusAspectRatio Read-only, not a DP. Ratio if positive, else 1.0.
Content object - ContentControl The single child, filled into the computed box via the default ContentPresenter; there is no separate inner element the way the web has an inner <div>.

NaviusAspectRatio.cs has no OnCreateAutomationPeer() override; the web contract renders no ARIA role or aria-* attribute either, so the parity doc’s own WPF strategy states no automation-peer role mapping is needed beyond whatever default ContentControl supplies. The width-vs-height-driven box math (NaviusAspectRatioMath.ComputeDesiredSize) is factored out into a pure static method and unit-tested headless with plain [Fact]s, with no live Control/Application required.

  • The web’s padding-bottom percentage-hack (an outer/inner <div> pair) has no WPF equivalent; this is a single ContentControl overriding MeasureOverride, computing the box directly rather than via a CSS trick.
  • ForwardedAttributes/style-merge (the web’s “spread the consumer’s style, then append positioning last”) has no port: the single child simply fills the computed box via the default ContentPresenter.
  • Width drives height whenever available width is finite (Width / EffectiveRatio, the direct analog of the web’s width:100%;padding-bottom:% hack). When width is infinite but height is finite, height instead drives width (Height * EffectiveRatio); when both are infinite, the box collapses to (0, 0). The web version is purely width-driven since a browser never presents an infinite-width layout, so the height-driven and both-infinite branches are WPF-specific additions with no web behavior to diverge from.

The AspectRatio Gallery page rendered at the pinned commit, in each theme.

AspectRatio Gallery page in the light theme

AspectRatio Gallery page in the dark theme

AspectRatio Gallery page in the high contrast theme