Back to Devexpress

FolderBrowserDialogService

wpf-114755-mvvm-framework-services-predefined-set-folderbrowserdialogservice.md

latest7.4 KB
Original Source

FolderBrowserDialogService

  • Aug 16, 2023
  • 3 minutes to read

The FolderBrowserDialogService is an IFolderBrowserDialogService implementation that allows you to browse, create, and select folders in the File System by using the standard folder browser dialog.

To utilize this service, attach the FolderBrowserDialogService to your View by using the Interaction.Behaviors collection.

xaml
<UserControl x:Class="FolderBrowserDialogServiceSample.Views.FolderBrowserDialogView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FolderBrowserDialogService />
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>

Then, access the attached service by using one of the approaches described in Services in POCO objects, Services in ViewModelBase descendants topics and invoke its IFolderBrowserDialogService.ShowDialog method to show the folder browser dialog.

csharp
public class FolderBrowserDialogViewModel {
    //...
    public virtual string ResultPath { get; set; }  
    protected virtual IFolderBrowserDialogService FolderBrowserDialogService { get { return this.GetService<IFolderBrowserDialogService>(); } }
    //...
    public void ShowDialog() {
        if (FolderBrowserDialogService.ShowDialog())
            ResultPath = FolderBrowserDialogService.ResultPath;
    }
}
vb
Public Class FolderBrowserDialogViewModel
    '...
    Public Overridable Property ResultPath() As String
    Protected Overridable ReadOnly Property FolderBrowserDialogService() As IFolderBrowserDialogService
        Get
            Return Me.GetService(Of IFolderBrowserDialogService)()
        End Get
    End Property
    '...
    Public Sub ShowDialog()
        If FolderBrowserDialogService.ShowDialog() Then
            ResultPath = FolderBrowserDialogService.ResultPath
        End If
    End Sub
End Class

To select a folder, a user should first select an item in the folder tree and press the OK button. When the dialog box is closed and the ShowDialog method returns true, the IFolderBrowserDialogService.ResultPath will be set to a string containing a path to the selected folder. Use the StartPath property to specify the initially selected folder.

To allow users to select a file instead of a folder, use the OpenFileDialogService.

Example

View Example

xaml
<UserControl x:Class="FolderBrowserDialogServiceSample.Views.FolderBrowserDialogView"
    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:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:FolderBrowserDialogServiceSample.ViewModels"
    mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
    DataContext="{dxmvvm:ViewModelSource Type=ViewModels:FolderBrowserDialogViewModel}">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FolderBrowserDialogService 
            Description="{Binding Description}"
            ShowNewFolderButton="{Binding ShowNewFolderButton}" 
            RootFolder="Desktop"
            RestorePreviouslySelectedDirectory="True"/>
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="10">
            <TextBlock 
                Text="Description: "
                Margin="3" 
                VerticalAlignment="Center"/>
            <TextBox 
                VerticalAlignment="Center" 
                Margin="3" 
                Width="80"
                Text="{Binding Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <CheckBox 
                VerticalAlignment="Center" 
                Margin="10" 
                Content="ShowNewFolderButton" 
                IsChecked="{Binding ShowNewFolderButton, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Button 
                Content="Show Dialog" 
                Command="{Binding ShowDialogCommand}" 
                Margin="10"/>
        </StackPanel>
        <TextBox 
            Text="{Binding ResultPath, Mode=OneWay}" 
            Grid.Row="1" 
            Margin="10"
            IsReadOnly="True" 
            AcceptsReturn="True"/>
    </Grid>
</UserControl>
csharp
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;

namespace FolderBrowserDialogServiceSample.ViewModels
{
    [POCOViewModel]
    public class FolderBrowserDialogViewModel {
        public virtual string Description { get; set; }
        public virtual bool ShowNewFolderButton { get; set; }
        public virtual string ResultPath { get; set; }
        protected virtual IFolderBrowserDialogService FolderBrowserDialogService { get { return this.GetService<IFolderBrowserDialogService>(); } }

        public FolderBrowserDialogViewModel() {
            ShowNewFolderButton = true;
        }
        public void ShowDialog() {
            if (FolderBrowserDialogService.ShowDialog())
                ResultPath = FolderBrowserDialogService.ResultPath;
        }
    }
}
vb
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.POCO

Namespace FolderBrowserDialogServiceSample.ViewModels
    <POCOViewModel> _
    Public Class FolderBrowserDialogViewModel
        Public Overridable Property Description() As String
        Public Overridable Property ShowNewFolderButton() As Boolean
        Public Overridable Property ResultPath() As String
        Protected Overridable ReadOnly Property FolderBrowserDialogService() As IFolderBrowserDialogService
            Get
                Return Me.GetService(Of IFolderBrowserDialogService)()
            End Get
        End Property

        Public Sub New()
            ShowNewFolderButton = True
        End Sub
        Public Sub ShowDialog()
            If FolderBrowserDialogService.ShowDialog() Then
                ResultPath = FolderBrowserDialogService.ResultPath
            End If
        End Sub
    End Class
End Namespace