Layout has two phases: measure asks children how much space they need; arrange assigns their final bounds. The parent determines the constraints a child sees. Understand that relationship before adding fixed widths or margins to compensate for unexpected behavior.
Try a bounded results area
Section titled “Try a bounded results area”This layout keeps the search field at its desired height and gives the remaining space to a scrolling list:
<Grid xmlns="https://github.com/ferroui" RowDefinitions="Auto,*" Height="320" Margin="16"> <TextBox PlaceholderText="Search records" Margin="0,0,0,12" /> <ListBox Grid.Row="1"> <ListBoxItem Content="Ada" /> <ListBoxItem Content="Grace" /> <ListBoxItem Content="Margaret" /> </ListBox></Grid>The fixed height makes this fragment easy to test. In a real resizable screen, let the window or parent supply the available height. Add enough items to overflow and reduce the height: the list should scroll while the search field remains visible. Compare the live Grid example at different preview widths.
Choose a panel
Section titled “Choose a panel”Use Grid for forms and resizable regions. Use StackPanel for a short flow in one direction. Use WrapPanel when items should continue on another line. Use DockPanel for edges around a remaining content region. Use Canvas when positioning is intrinsically coordinate-based.
<Grid xmlns="https://github.com/ferroui" ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" Margin="24"> <TextBlock Text="Name" Margin="0,0,12,8" /> <TextBox Grid.Column="1" PlaceholderText="Display name" /> <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Content="Save" /></Grid>The same form in declarative Rust: grid rows and columns take the XAML notation, attached properties use their owner’s name, and a tuple or a number converts to a Thickness:
use ferroui_base::layout::HorizontalAlignment;use ferroui_view::prelude::*;
let form = rsx! { Grid { column_definitions: "Auto,*", row_definitions: "Auto,Auto", margin: 24.0, TextBlock { text: "Name", margin: (0.0, 0.0, 12.0, 8.0) } TextBox { Grid.column: 1, placeholder_text: "Display name" } Button { Grid.row: 1, Grid.column: 1, horizontal_alignment: HorizontalAlignment::Left, "Save" } }};Constraints and scrolling
Section titled “Constraints and scrolling”A StackPanel can offer effectively unbounded space along its stacking direction. A virtualizing list needs a bounded viewport to know which containers to realize. Put the list in a constrained Grid row instead of an outer scrolling StackPanel.
Use MinWidth, MaxWidth, and alignment to express intent. Fixed pixel coordinates are fragile when text changes, localization makes labels longer, or a user increases the font size.
Margin, padding, and alignment
Section titled “Margin, padding, and alignment”Margin reserves space outside a control; padding reserves space around its content. Alignment positions the control within its allocation. A stretched control with a fixed width remains constrained by that width.
Inspect actual bounds in the developer tools when something appears clipped. Check the parent first, then the child, and look for an earlier fixed-size constraint rather than adding more clipping exceptions.
When the layout still looks wrong
Section titled “When the layout still looks wrong”| Result | Check |
|---|---|
| The list grows instead of scrolling | Does its parent give it a finite height? |
| A field will not stretch | Is a fixed Width or parent column constraining it? |
| Text clips after localization | Is a fixed height preventing wrapping? |
| Spacing doubles unexpectedly | Are both adjacent margins reserving space? |
Try the controls
Section titled “Try the controls”Change the column definitions and preview width. Compare the result with the sizing rules above.
Real controls, running in WebAssembly.
The runtime downloads when you press Run.
<Grid xmlns="https://github.com/ferroui" ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" Margin="12"> <TextBlock Text="Name" Margin="0,0,16,12" /> <TextBox Grid.Column="1" PlaceholderText="Your name" /> <TextBlock Grid.Row="1" Text="Team" Margin="0,0,16,0" /> <TextBox Grid.Row="1" Grid.Column="1" PlaceholderText="Your team" /></Grid>use ferroui::prelude::*;use ferroui::base::metadata::from_markup_value;use ferroui::markup_xaml_loader::FerroRuntimeXamlLoader;
#[repr(C)]pub struct App { base: Application,}
ferro_class!(App: Application);ferro_impl_classes!(App: FerroObjectImpl);
impl NewApplication for App { fn new_application() -> Ref<Self> { instantiate(Self { base: Application::construct() }) }}
impl ApplicationImpl for App { fn initialize(this: &Self) { Self::parent_initialize(this); ferroui::controls::register_types(); FerroRuntimeXamlLoader::register(); this.styles().add(FluentTheme::new().as_style()); }
fn on_framework_initialization_completed(this: &Self) { let lifetime = this.application_lifetime(); if let Some(desktop) = lifetime.as_ref().and_then(|l| l.as_classic_desktop_style_application_lifetime()) { let value = FerroRuntimeXamlLoader::load(include_str!("View.xaml"), None, None, None, false) .expect("Example XAML should load"); let view = from_markup_value::<Ref<Control>>(&Some(value)).expect("Example root must be a control"); let window = Window::new(); window.set_title(Some("FerroUI documentation example".to_owned())); window.set_width(560.0); window.set_height(420.0); window.set_padding(Thickness::uniform(24.0)); window.set_content(Some(Control::boxed(view))); desktop.set_main_window(Some(window)); } Self::parent_on_framework_initialization_completed(this); }}
fn main() -> std::process::ExitCode { let args: Vec<String> = std::env::args().skip(1).collect(); let result = AppBuilder::configure::<App>().use_platform_detect().start_with_classic_desktop_lifetime(&args); std::process::ExitCode::from(result as u8)}Public APIs and controls
Section titled “Public APIs and controls”- Grid, StackPanel, WrapPanel, and DockPanel — layout strategies.
- ScrollViewer and ListBox — scrolling content.
Control— sizing, alignment, and bounds.GridDefinitions— grid rows and columns from declarative Rust.- Developer tools — inspect actual constraints and values.