Skip to content
FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    Build a view model with ReactiveUI

    Use MVVM when several views display or edit the same application state, or when views are written in XAML. The model holds domain data, the view model exposes state and actions, and the view binds controls to them. You can test a view model without opening a window. FerroUI’s ReactiveUI, DynamicData, and Rx libraries are crates of the release; only their integration with the controls depends on the user interface.

    MVVM is one way to structure state. A declarative application can instead keep view state in signals and hooks and call services directly (components and hooks), and the two meet in the middle: a component can read a view model from its data context. The ReactiveDemo sample exists in both forms, as a view model with XAML views and as components.

    This guide builds the state for a search field. Start with a working application; keep the ReactiveUI dependencies at the same version as the rest of FerroUI.

    A plain Rust field does not notify bindings. reactive_properties! creates getters and setters that raise change notifications; reactive_object! implements the reactive notification traits.

    use std::rc::Rc;
    use ferroui_reactiveui::{reactive_object, reactive_properties, ReactiveObject};
    reactive_properties! {
    #[derive(Default)]
    pub struct SearchViewModel {
    reactive: ReactiveObject,
    #[reactive]
    search_text: String,
    }
    }
    reactive_object!(SearchViewModel);
    let model = Rc::new(SearchViewModel::default());
    model.set_search_text("FerroUI".to_owned());
    assert_eq!(model.search_text(), "FerroUI");

    The Rust getter is search_text; its notification name is SearchText. Keep that name consistent with the property you expose to XAML. The state above is usable from Rust; XAML needs the registration in the next step as well.

    Application types need markup metadata. The reactive macro supplies notifications, but it does not register a XAML property. For a complete registration example, follow DynamicDataViewModel: its ferro_markup_type! declaration exposes SearchText with a getter and setter and uses the same notification name.

    Register the application’s types before loading markup. Give the view the model as its DataContext, or use a typed reactive view and its ViewModel property as the samples do. Then the view can read and edit the registered property. This fragment explicitly uses reflection; for compiled markup, prefer a typed binding once the registered type is known:

    <StackPanel xmlns="https://github.com/ferroui" Spacing="8">
    <TextBlock Text="Search packages" />
    <TextBox Text="{ReflectionBinding SearchText, Mode=TwoWay}"
    PlaceholderText="Package name" />
    <TextBlock Text="{ReflectionBinding SearchText}" />
    </StackPanel>

    Typing should change the model and the second label. If it does not, check DataContext, the registered getter/setter, and SearchText notifications in that order. The binding guide includes a runnable named-control example to isolate binding behavior from model registration.

    3. Initialize ReactiveUI with the application

    Section titled “3. Initialize ReactiveUI with the application”

    The desktop builder must install the ReactiveUI integration before views start using its UI scheduler. This fragment belongs in your existing application builder; App is your application type.

    use ferroui_controls::AppBuilder;
    use ferroui_desktop::AppBuilderDesktopExtensions;
    use ferroui_reactiveui::ReactiveUIBuilder;
    use ferroui_reactiveui_controls::AppBuilderExtensions;
    let builder = AppBuilder::configure::<App>()
    .use_platform_detect()
    .use_reactive_ui(|_: &ReactiveUIBuilder| {});

    If the markup uses reactive controls, also call ferroui_reactiveui_controls::register_types() during type registration. The ReactiveDemo entry point shows the complete startup path.

    Need Use Next guide
    A Save or Search action ReactiveCommand, adapted for a control’s Command property Commands
    A value calculated from other properties when_any_value and to_property Binding
    A filtered or sorted list SourceCache and a notifying output collection Collections
    A request that completes later A task, loading/error state, and cancellation Async work
    A view chosen from a view-model type View registration and ViewModelViewHost Navigation

    Keep services such as file access or searching behind ordinary Rust interfaces and pass them into the model. A view model should not need to locate a TextBox to learn the query.

    Create view bindings inside when_activated and add their disposables to the activation’s collection. Deactivation releases those bindings. Keep long-lived model state separate from view subscriptions so navigating away does not leak handlers or discard persisted data accidentally.

    View models and reactive observables use Rc and stay on their owning thread. Send plain data to workers, then create or update view models on the UI thread. See async work before using a background command.

    A component mounted under a view with a data context reads it with use_data_context::<T>() and runs again when it changes. For example, a component that receives an Rc<SearchViewModel> as its data context can read the model, call its methods from handlers, and show its state. The reactiveui feature of ferroui-view adds builders for the ReactiveUI controls (view_model_view_host(), routed_view_host()). See components with XAML.

    From the matching source checkout:

    Terminal window
    cargo run -p reactive-demo --no-default-features
    cargo test -p reactive-demo

    The sample searches an in-memory catalog in this configuration. Type a query, wait for the debounce, select a result, and inspect the view-model tests. ReactiveUIExample adds commands, routing, and changing collections. Compare ReactiveDemoComponents (cargo run -p reactive-demo-components --no-default-features), the same crate browser written as components: what the view model computes with when_any_value and to_property is computed there with hooks and use_future.