Skip to content
FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    Bind controls to application state

    A control property can follow application state in two ways. In declarative Rust, a property follows a signal, a memo, or a closure directly, and a two-way value writes edits back. In XAML, and from Rust through the property system, a binding connects a target property to a value on a source object, usually the current DataContext; the source must expose the properties and notifications FerroUI’s binding system expects.

    use ferroui_view::prelude::*;
    #[component]
    pub fn Search() -> Element {
    let search_text = signal(String::new());
    rsx! {
    StackPanel { spacing: 12.0,
    TextBox { text: search_text.two_way(), placeholder_text: "Search" }
    TextBlock { text: search_text }
    TextBlock { text: move || format!("{} characters", search_text.with(|text| text.chars().count())) }
    }
    }
    }

    Typing writes into search_text; both text blocks follow it, and nothing else runs. There is no notification to raise and no name to register: the signal is the source, and the compiler checks the property names and value types. A closure recalculates when the signals it reads change. In a component that runs again, text: format!(..) with a value read in the function also works: the component runs and the reconciler sets the new text.

    To bind an element to a view model instead, .bind(property, binding) sets a binding of the property system on any element, and use_data_context reads the data context in a component (components with XAML).

    <StackPanel xmlns="https://github.com/ferroui" Spacing="12">
    <TextBox Text="{ReflectionBinding SearchText, Mode=TwoWay}" />
    <TextBlock Text="{ReflectionBinding SearchText}" />
    </StackPanel>

    This example binds to another named control rather than a view model. Edit the text field in the running preview: the label follows the same public Text property and notification system used by view-model bindings.

    A binding that updates as you typeFerroUI · 0.5.0
    Try it for yourself

    Real controls, running in WebAssembly.
    The runtime downloads when you press Run.

    Shared runtime · 9.8 MB compressed · cached across examples
    Download project
    <StackPanel xmlns="https://github.com/ferroui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Spacing="12">
    <TextBlock Text="Edit the field to update the label" />
    <TextBox x:Name="Editor" Text="Hello, FerroUI" />
    <TextBlock x:Name="Echo" Text="{Binding Text, ElementName=Editor}" FontSize="20" />
    </StackPanel>
    Ready to explore

    Use one-way binding for values the UI only displays. Use two-way binding when a control edits view-model state. A default binding mode can be property-specific; write Mode=TwoWay explicitly while learning or debugging a form.

    A value changing in memory does not automatically update the UI. The source must notify the binding system. With ReactiveUI, use the reactive property macros or the corresponding change-notification APIs. See MVVM.

    Binding paths use the markup-facing property names from registration. Rust’s search_text field convention does not imply that SearchText is registered automatically. A reactive property’s declared notification name and its markup metadata must agree.

    Use an explicit source or named-control binding when the value belongs to another control rather than the view model. Avoid replacing a container’s data context just to bind one child: doing so changes every descendant binding that inherits that context.

    Build-time markup compiles binding paths by default and needs source type information. The untyped examples above choose ReflectionBinding explicitly. See compiled binding setup for the typed form and runtime loader configuration. If a path works in an imperative Rust call but not in XAML, check registration, source type, property name, and notification behavior.

    1. Confirm that the control has the intended DataContext.
    2. Inspect the binding path and its markup-facing names.
    3. Confirm that the source exposes a getter and, when editing, a setter.
    4. Change the source and verify a notification is raised.
    5. Check the developer tools for the target’s effective value and value source.

    Use converters for presentation transformations. Keep business validation and domain decisions in the model or view model so they remain testable without a UI.

    Example Source Use it when
    {ReflectionBinding SearchText} Inherited data context The value belongs to the view model
    {ReflectionBinding SearchText, Mode=TwoWay} Writable data-context property The user edits model state
    {Binding #Query.Text} Named control Query One view element follows another

    For a model binding, start by setting the property in Rust and confirming the view updates; then edit the control and confirm the setter runs. This separates missing notifications from a missing two-way path. See forms for the full input, validation, and submit contract.