SaveFileDialogService

  • 5 minutes to read

The SaveFileDialogService is an ISaveFileDialogService implementation that allows you to save data of a ViewModel to a file by using the standard dialog box.

SaveFileDialogServiceImage

To use the SaveFileDialogService, attach it to a View as described in Attaching MVVM Behaviors and Services.

<UserControl x:Class="FileDialogServicesSample.Views.FileDialogsView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:SaveFileDialogService />
    </dxmvvm:Interaction.Behaviors>
</UserControl>

Next, access the attached service and use the service's ISaveFileDialogService.ShowDialog method to display a dialog box.

[POCOViewModel]
public class FileDialogsViewModel {

    protected ISaveFileDialogService SaveFileDialogService { get { return this.GetService<ISaveFileDialogService>(); } }
    ...
    public void Save() {
        ...
        if (SaveFileDialogService.ShowDialog()) {
            ...
        }
    }
}

A description on how to obtain a service from a View Model is available here: Services in POCO objects and Services in ViewModelBase descendants.

Then, use the FileInfoWrapper object that is stored in the ISaveFileDialogService.File property to save your data. By default, the SaveFileDialogService uses values of DefaultFileName and SaveFileDialogService.DefaultExt properties to name the file that will be used as the ViewModel's data storage. However, an end-user can specify the file name manually by using the corresponding dialog box fields.

NOTE

The SaveFileDialogService uses its own FileInfoWrapper class implementing the IFileInfo interface to represent the selected file(s). This class provides the capabilities of the standard FileInfo to work with FileStream objects.

Example

Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm.POCO
Imports System.IO
Imports System.Linq

Namespace FileDialogServicesSample.ViewModels
    <POCOViewModel> _
    Public Class FileDialogsViewModel
        #Region "Common properties"
        Public Overridable Property Filter() As String
        Public Overridable Property FilterIndex() As Integer
        Public Overridable Property Title() As String
        Private privateDialogResult As Boolean
        Public Overridable Property DialogResult() As Boolean
            Get
                Return privateDialogResult
            End Get
            Protected Set(ByVal value As Boolean)
                privateDialogResult = value
            End Set
        End Property
        Private privateResultFileName As String
        Public Overridable Property ResultFileName() As String
            Get
                Return privateResultFileName
            End Get
            Protected Set(ByVal value As String)
                privateResultFileName = value
            End Set
        End Property
        Public Overridable Property FileBody() As String
        #End Region

        #Region "SaveFileDialogService specific properties"
        Public Overridable Property DefaultExt() As String
        Public Overridable Property DefaultFileName() As String
        Public Overridable Property OverwritePrompt() As Boolean
        #End Region

        Protected ReadOnly Property SaveFileDialogService() As ISaveFileDialogService
            Get
                Return Me.GetService(Of ISaveFileDialogService)()
            End Get
        End Property
        Protected ReadOnly Property OpenFileDialogService() As IOpenFileDialogService
            Get
                Return Me.GetService(Of IOpenFileDialogService)()
            End Get
        End Property

        Public Sub New()
            Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*"
            FilterIndex = 1
            Title = "Custom Dialog Title"
            DefaultExt = "txt"
            DefaultFileName = "Document1"
            OverwritePrompt = True
        End Sub

        Public Sub Open()
            OpenFileDialogService.Filter = Filter
            OpenFileDialogService.FilterIndex = FilterIndex
            DialogResult = OpenFileDialogService.ShowDialog()
            If Not DialogResult Then
                ResultFileName = String.Empty
            Else
                Dim file As IFileInfo = OpenFileDialogService.Files.First()
                ResultFileName = file.Name
                Using stream = file.OpenText()
                    FileBody = stream.ReadToEnd()
                End Using
            End If
        End Sub

        Public Sub Save()
            SaveFileDialogService.DefaultExt = DefaultExt
            SaveFileDialogService.DefaultFileName = DefaultFileName
            SaveFileDialogService.Filter = Filter
            SaveFileDialogService.FilterIndex = FilterIndex
            DialogResult = SaveFileDialogService.ShowDialog()
            If Not DialogResult Then
                ResultFileName = String.Empty
            Else
                Using stream = New StreamWriter(SaveFileDialogService.OpenFile())
                    stream.Write(FileBody)
                End Using
            End If
        End Sub

    End Class
End Namespace