The OpenFileDialogService is an IOpenFileDialogService implementation that allows you to browse and open files in the File System by using the standard dialog box.

To implement the file browsing functionality in accordance with the MVVM pattern, use the OpenFileDialogService class provided by MVVM Framework.
Using the OpenFileDialogService functionality requires a OpenFileDialogService instance to be attached to a View. You can do this by using the Smart Tag as described in Attaching MVVM Behaviors and Services or manually as shown in the code snippet below.
XAML |
<UserControl x:Class="FileDialogServicesSample.Views.FileDialogsView"
...
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:OpenFileDialogService />
</dxmvvm:Interaction.Behaviors>
</UserControl>
|
To show the dialog box, access the attached OpenFileDialogService (a description on how to obtain a service from a View Model is available here: Services in POCO objects and Services in ViewModelBase descendants) and invoke its IOpenFileDialogService.ShowDialog method.
C# |
[POCOViewModel]
public class FileDialogsViewModel {
protected IOpenFileDialogService OpenFileDialogService { get { return this.GetService<IOpenFileDialogService>(); } }
...
public void Open() {
if (OpenFileDialogService.ShowDialog()) {
...
}
}
}
|
VB |
<POCOViewModel> _
Public Class FileDialogsViewModel
Protected ReadOnly Property OpenFileDialogService() As IOpenFileDialogService
Get
Return Me.GetService(Of IOpenFileDialogService)()
End Get
End Property
...
Public Sub Open()
...
If OpenFileDialogService.ShowDialog() Then
...
End If
End Sub
End Class
|
When the shown dialog is closed and the ShowDialog method returns True, you can obtain the selected file in the IOpenFileDialogService.File property. If the OpenFileDialogService.Multiselect feature is enabled and multiple files are selected, use the IOpenFileDialogService.Files property instead.
Note
The OpenFileDialogService 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 objects of the FileStream type.
If you want to enable the user with the ability to select a folder instead of a file, use the FolderBrowserDialogService instead.

Example
FileDialogsViewModel.vb |
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
|
FileDialogsView.xaml |
<UserControl
x:Class="FileDialogServicesSample.Views.FileDialogsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:ViewModels="clr-namespace:FileDialogServicesSample.ViewModels"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
DataContext="{dxmvvm:ViewModelSource Type={x:Type ViewModels:FileDialogsViewModel}}">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:OpenFileDialogService Title="{Binding Title}"/>
<dxmvvm:SaveFileDialogService Title="{Binding Title}"/>
</dxmvvm:Interaction.Behaviors>
<DockPanel>
<dxr:RibbonControl
DockPanel.Dock="Top"
ShowApplicationButton="False"
ToolbarShowMode="Hide"
RibbonTitleBarVisibility="Collapsed">
<dxr:RibbonDefaultPageCategory Caption="Default Category">
<dxr:RibbonPage Caption="File">
<dxr:RibbonPageGroup Caption="Actions">
<dxb:BarButtonItem
Content="Open"
Command="{Binding OpenCommand}"
Glyph="{dx:DXImage Image=Open_16x16.png}"
LargeGlyph="{dx:DXImage Image=Open2_32x32.png}"
RibbonStyle="Large"/>
<dxb:BarButtonItem
Content="Save"
Command="{Binding SaveCommand}"
Glyph="{dx:DXImage Image=Save_16x16.png}"
LargeGlyph="{dx:DXImage Image=Save_32x32.png}"
RibbonStyle="Large"/>
</dxr:RibbonPageGroup>
<dxr:RibbonPageGroup Caption="Settings">
<dxb:BarEditItem Content="Title" EditValue="{Binding Title}">
<dxb:BarEditItem.EditSettings>
<dxe:TextEditSettings/>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
<dxb:BarEditItem Content="Filter" EditValue="{Binding Filter}">
<dxb:BarEditItem.EditSettings>
<dxe:TextEditSettings/>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
<dxb:BarEditItem Content="Filter Index" EditValue="{Binding FilterIndex}">
<dxb:BarEditItem.EditSettings>
<dxe:TextEditSettings/>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
</dxr:RibbonPageGroup>
</dxr:RibbonPage>
</dxr:RibbonDefaultPageCategory>
</dxr:RibbonControl>
<dxr:RibbonStatusBarControl DockPanel.Dock="Bottom">
<dxr:RibbonStatusBarControl.RightItems>
<dxb:BarStaticItem Content="{Binding DialogResult}">
<dxb:BarStaticItem.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Dialog Result: "/>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</dxb:BarStaticItem.ContentTemplate>
</dxb:BarStaticItem>
</dxr:RibbonStatusBarControl.RightItems>
</dxr:RibbonStatusBarControl>
<TextBox
Text="{Binding FileBody, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AcceptsReturn="True"
TextWrapping="Wrap"/>
</DockPanel>
</UserControl>
|
FileDialogsViewModel.cs |
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.POCO;
using System.IO;
using System.Linq;
namespace FileDialogServicesSample.ViewModels
{
[POCOViewModel]
public class FileDialogsViewModel {
#region Common properties
public virtual string Filter { get; set; }
public virtual int FilterIndex { get; set; }
public virtual string Title { get; set; }
public virtual bool DialogResult { get; protected set; }
public virtual string ResultFileName { get; protected set; }
public virtual string FileBody { get; set; }
#endregion
#region SaveFileDialogService specific properties
public virtual string DefaultExt { get; set; }
public virtual string DefaultFileName { get; set; }
public virtual bool OverwritePrompt { get; set; }
#endregion
protected ISaveFileDialogService SaveFileDialogService { get { return this.GetService<ISaveFileDialogService>(); } }
protected IOpenFileDialogService OpenFileDialogService { get { return this.GetService<IOpenFileDialogService>(); } }
public FileDialogsViewModel() {
Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
FilterIndex = 1;
Title = "Custom Dialog Title";
DefaultExt = "txt";
DefaultFileName = "Document1";
OverwritePrompt = true;
}
public void Open() {
OpenFileDialogService.Filter = Filter;
OpenFileDialogService.FilterIndex = FilterIndex;
DialogResult = OpenFileDialogService.ShowDialog();
if (!DialogResult) {
ResultFileName = string.Empty;
} else {
IFileInfo file = OpenFileDialogService.Files.First();
ResultFileName = file.Name;
using (var stream = file.OpenText()) {
FileBody = stream.ReadToEnd();
}
}
}
public void Save() {
SaveFileDialogService.DefaultExt = DefaultExt;
SaveFileDialogService.DefaultFileName = DefaultFileName;
SaveFileDialogService.Filter = Filter;
SaveFileDialogService.FilterIndex = FilterIndex;
DialogResult = SaveFileDialogService.ShowDialog();
if (!DialogResult) {
ResultFileName = string.Empty;
} else {
using (var stream = new StreamWriter(SaveFileDialogService.OpenFile())) {
stream.Write(FileBody);
}
}
}
}
}
|