UIObjectService
- 2 minutes to read
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.
Attach the Service
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>
Access the Service
Refer to the following topics for more information on how to access a service in a ViewModel:
- Services in ViewModelBase Descendants
- Services in Generated View Models
- Services in Custom ViewModels
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");
}
}
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.