- 1 year ago
Bind view and model in caliburn micro
Comment
1 / 1
To bind the view and view model in Caliburn.Micro, you can follow these steps:
Create a View:
- Create a XAML file that represents the visual layout of your view.
- Set the
x:Class
attribute of the root element to match the name of your view class.
Create a ViewModel:
- Create a separate class that serves as the view model for your view.
- Implement the necessary properties and methods in the view model.
Establish Naming Conventions:
- By default, Caliburn.Micro follows naming conventions to automatically associate views and view models.
- The view model class should have the same name as the view class but with the "ViewModel" suffix.
- For example, if your view is named
MainView.xaml
, the corresponding view model should be namedMainViewModel.cs
.
Add Bindings:
- In your view XAML, use Caliburn.Micro's binding conventions to connect elements to properties/methods in the view model.
- For example, to bind a button's
Click
event to a command in the view model, you can use thecal:Message.Attach
syntax.
Register View and ViewModel:
- In your application startup, register the views and view models with the Caliburn.Micro framework.
- You can do this in the
Configure
method of your bootstrapper class by calling thecontainer.RegisterPerRequest
method.
Display the View:
- Use Caliburn.Micro's
IWindowManager
to display the view. - Typically, you would call the
IWindowManager.ShowWindow
method with an instance of your view model to display the associated view.
- Use Caliburn.Micro's
By following these steps, Caliburn.Micro will automatically handle the binding between the view and view model based on naming conventions and the specified bindings. Changes in the view model will be reflected in the view, and actions in the view (e.g., button clicks) can be handled by the corresponding methods in the view model.