wpf-404099-mvvm-framework-services-predefined-set-ui-object-service.md
You can attach a UIObjectService to an element in a View. This service allows you to access that element from View Model code. The service does not reference the element’s type to preserve the MVVM pattern.
This service implements the IUIObjectService interface.
Assign the UIObjectService to a UI element in any of the following ways:
Use the element’s Quick Actions menu to add the UIObjectService.
Declare the UIObjectService in the markup.
<dxg:GridControl ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView ...>
<dxmvvm:Interaction.Behaviors>
<dxmvvm:UIObjectService/>
</dxmvvm:Interaction.Behaviors>
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
Refer to the following topics for more information on how to access a service in a ViewModel:
The following code sample shows how to access the service from a View Model that is a ViewModelBase descendant:
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
public class MainViewModel : ViewModelBase {
// ...
public IUIObjectService TableViewService { get { return GetService<IUIObjectService>(); } }
[Command]
public void ExportToPDF() {
TableViewService.Object.ExportToPdf(@"C:\Work\Documents\GridExport.pdf");
}
}
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Public Class MainViewModel
Inherits ViewModelBase
' ...
Public ReadOnly Property TableViewService As IUIObjectService
Get
Return GetService(Of IUIObjectService)()
End Get
End Property
<Command>
Public Sub ExportToPDF()
TableViewService.Object.ExportToPdf("C:\Work\Documents\GridExport.pdf")
End Sub
End Class
The Object property returns the associated object of the dynamic type to not reference the object’s real type in the View Model and maintain a clean MVVM pattern.