Skip to main content
All docs
V24.1

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.

Run Demo: UIObjectService

Attach the Service

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

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

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.