wpf-16920-mvvm-framework-services-create-a-custom-service.md
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:
Create a service interface with methods that should be called from the View Model:
Create a ServiceBase class descendant that implements your interface:
Use the AssociatedObject property to access the associated control:
Add your service to the Behaviors collection to attach it to a control:
Access your service in the View Model:
(Optional) Add a DependencyProperty to your service. This property is set at the View level and you can use its value within the service:
(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