FolderBrowserDialogService
- 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.
<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 approaches described in Services in POCO objects, Services in ViewModelBase descendants topics and invoke its IFolderBrowserDialogService.ShowDialog method to show the folder browser dialog.
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;
}
}
To select a folder, a user should select an item in the folders tree and press the OK button. When the dialog box is closed and the ShowDialog returns true, the IFolderBrowserDialogService.ResultPath will be set to a string containing a path to the selected folder.
If you want to enable the user the ability to select a file instead of a folder, use the OpenFileDialogService instead.
Example
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-use-folderbrowserdialogservice-t298257.
<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>