# view_for

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-reactiveui/macro-view_for/

**macro** · [`ferroui-reactiveui`](/docs/0.5.0/api/ferroui-reactiveui/) · v0.5.0

```rust
macro_rules! view_for
```

Implements `IActivatableView`,
`IViewFor` and
`IViewForOf<VM>` for a view.

Not an upstream item: upstream's views write the two `ViewModel` properties by hand (the
untyped one casting to the typed one).

- `view_for!(View, ViewModel)`: the view has inherent accessors
  `view_model(&self) -> Option<Rc<ViewModel>>` and `set_view_model(&self, Option<Rc<ViewModel>>)`,
  written by hand or by `reactive_properties!` with
  `#[reactive] view_model: Option<Rc<ViewModel>>`.
- `view_for!(View, ViewModel, field)`: the view holds its view model in
  `field: RefCell<Option<Rc<ViewModel>>>`, and the macro writes the inherent accessors too.
- `view_for!(View: INotifyPropertyChanged, ViewModel)`: as the first form, for a view that
  raises `PropertyChanged` (a reactive object): the property observation of its `ViewModel`
  (the bindings and the activation of the view) follows the changes of its view model through
  `IActivatableView::as_notify_property_changed`.

The untyped setter panics when it is given a view model of another type, as the cast of
upstream's views throws an `InvalidCastException`.

```
use ferroui_reactiveui::view_for;
use ferroui_reactiveui::binding::interfaces::IViewFor;
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;

struct TodoViewModel;

#[derive(Default)]
struct TodoView {
    view_model: RefCell<Option<Rc<TodoViewModel>>>,
}
view_for!(TodoView, TodoViewModel, view_model);

let view = Rc::new(TodoView::default());
IViewFor::set_view_model(&*view, Some(Rc::new(TodoViewModel) as Rc<dyn Any>));
assert!(view.view_model().is_some());
```

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.ReactiveUI/binding/interfaces/i_view_for_t.rs#L62)

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
