Back to Devexpress

UIObjectService Class

wpf-devexpress-dot-mvvm-dot-ui-84479fef.md

latest5.0 KB
Original Source

UIObjectService Class

Allows you to write View Model code that can access UI objects in a View and does not reference the object’s type.

Namespace : DevExpress.Mvvm.UI

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
[TargetType(typeof(FrameworkElement))]
public class UIObjectService :
    ServiceBase,
    IUIObjectService
vb
<TargetType(GetType(FrameworkElement))>
Public Class UIObjectService
    Inherits ServiceBase
    Implements IUIObjectService

Remarks

Run Demo: UIObjectService

The UIObjectService implements the IUIObjectService interface that has the following declaration:

csharp
public interface IUIObjectService {
    dynamic Object { get; }
}
vb
Interface IUIObjectService
    Private ReadOnly Property Object As dynamic
End Interface

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.

Example

The following code sample uses the UIObjectService to access the GridControl and TableView objects:

xaml
<dxg:GridControl ItemsSource="{Binding Source}" AutoGenerateColumns="AddNew">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:UIObjectService x:Name="gridControlService"/>
    </dxmvvm:Interaction.Behaviors>
    <dxg:GridControl.View>
        <dxg:TableView ...>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:UIObjectService x:Name="tableViewService"/>
            </dxmvvm:Interaction.Behaviors>
        </dxg:TableView>
    </dxg:GridControl.View>
</dxg:GridControl>
<Button Content="Export to PDF" Command="{Binding ExportToPdfCommand}"/>
<Button Content="Filter" Command="{Binding ApplyFilterCommand}"/>
csharp
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;

public class MainViewModel : ViewModelBase {
    // ...
    public IUIObjectService GridService { get { return GetService<IUIObjectService>("gridControlService"); } }
    public IUIObjectService TableViewService { get { return GetService<IUIObjectService>("tableViewService"); } }

    [Command]
    public void ApplyFilter() {
        GridService.Object.FilterString = "[Quantity] >= 50";
    }
    [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 GridService As IUIObjectService
        Get
            Return GetService(Of IUIObjectService)("gridControlService")
        End Get
    End Property
    Public ReadOnly Property TableViewService As IUIObjectService
        Get
            Return GetService(Of IUIObjectService)("tableViewService")
        End Get
    End Property

    <Command>
    Public Sub ApplyFilter()
        GridService.Object.FilterString = "[Quantity] >= 50"
    End Sub
    <Command>
    Public Sub ExportToPdf()
        TableViewService.Object.ExportToPdf("C:\Work\Documents\GridExport.pdf")
    End Sub
End Class

Refer to the following help topic for more information: UIObjectService.

Inheritance

Show 11 items

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<FrameworkElement> DevExpress.Mvvm.UI.ServiceBaseGeneric<FrameworkElement> ServiceBase UIObjectService

See Also

UIObjectService Members

DevExpress.Mvvm.UI Namespace