Back to Devexpress

UIObjectService

wpf-404099-mvvm-framework-services-predefined-set-ui-object-service.md

latest3.0 KB
Original Source

UIObjectService

  • Sep 26, 2022
  • 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.

Run Demo: UIObjectService

Attach the Service

Assign the UIObjectService to a UI element in any of the following ways:

xaml
<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:

The following code sample shows how to access the service from a View Model that is a ViewModelBase descendant:

csharp
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");
    }
}
vb
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.