Back to Devexpress

Create a Custom Service

wpf-16920-mvvm-framework-services-create-a-custom-service.md

latest1.8 KB
Original Source

Create a Custom Service

  • Aug 10, 2023
  • 4 minutes to read

This topic describes how to create a custom service if predefined services do not suit your requirements.

Note

If you need to implement the logic that should access a UI element in the View Model code, you can use the UIObjectService instead.

Follow the steps below to create a custom service, attach it to a control, and access this service in the View Model:

  1. Create a service interface with methods that should be called from the View Model:

  2. Create a ServiceBase class descendant that implements your interface:

  3. Use the AssociatedObject property to access the associated control:

  4. Add your service to the Behaviors collection to attach it to a control:

  5. Access your service in the View Model:

  6. (Optional) Add a DependencyProperty to your service. This property is set at the View level and you can use its value within the service:

  7. (Optional) Override OnAttached and OnDetaching methods to subscribe and unsubscribe from associated control events:

The complete sample project is available in the following GitHub repository:

View Example: Data Grid for WPF - How to Update Data in a Separate Thread

See Also

Create a Custom Behavior

Example: Create a Custom Service