Back to Devexpress

BrowsePathEdit Class

wpf-devexpress-dot-xpf-dot-editors-a474cd7a.md

latest9.7 KB
Original Source

BrowsePathEdit Class

A text editor that allows users to specify a path to a file/folder.

Namespace : DevExpress.Xpf.Editors

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

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
[DXLicenseWpf]
public class BrowsePathEdit :
    ButtonEdit
vb
<DXLicenseWpf>
Public Class BrowsePathEdit
    Inherits ButtonEdit

Remarks

The BrowsePathEdit displays the specified file’s/folder’s icon, a text box where users can edit the path, and a button that invokes a browse dialog.

Run Demo: BrowsePathEdit

Tip

The BrowsePathEdit class inherits its features from the ButtonEdit class.

Refer to the ButtonEdit class description for information on derived features and API.

Create a BrowsePathEdit

Use the BaseEdit.EditValue property to specify the editor’s value.

xaml
<Window ...
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">

<dxe:BrowsePathEdit EditValue="C:\Program Files\"/>

<dxe:BrowsePathEdit EditValue="C:\"/>

Handle the BaseEdit.EditValueChanged event to get a notification when the editor’s value is changed. To validate the new value, handle the BaseEdit.Validate event or use the techniques described in the following help topic: Input Validation.

Customize the Path Icon

The BrowsePathEdit displays a path icon if the ShowIcon property is set to true. The PathIcon property returns the displayed icon.

If a user specifies an invalid file path, the BrowsePathEdit displays the invalid path icon:

Path Icon Selector

The following code sample uses the PathIconSelector property to change the icon displayed for folders:

xaml
<Window.Resources>
    <local:IconSelector x:Key="iconSelector"/>
</Window.Resources>

<!-- ... -->

<dxe:BrowsePathEdit DialogType="Folder" PathIconSelector="{StaticResource iconSelector}">
csharp
using DevExpress.Xpf.Editors;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
// ...

public class IconSelector : IPathIconSelector {
    public ImageSource Select(string path) {
        if (Directory.Exists(path))
            return new BitmapImage(new System.Uri("pack://application:,,,/Open_16x16.png"));
        return null;
    }
}
vb
Imports DevExpress.Xpf.Editors
Imports System.IO
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
' ...

Public Class IconSelector
    Inherits IPathIconSelector

    Public Function [Select](ByVal path As String) As ImageSource
        If Directory.Exists(path) Then Return New BitmapImage(New System.Uri("pack://application:,,,/Open_16x16.png"))
        Return Nothing
    End Function
End Class

CustomPathIcon Event

The following code sample uses the CustomPathIcon event to change the icon displayed if a user enters an invalid path:

xaml
<dxe:BrowsePathEdit DialogType="Folder" CustomPathIcon="OnCustomPathIcon">
csharp
using DevExpress.Xpf.Editors;
using System.IO;
using System.Windows.Media.Imaging;
// ...

void OnCustomPathIcon(object sender, PathIconEventArgs e) {
    if (!Directory.Exists(e.Path))
        e.Icon = new BitmapImage(new System.Uri("pack://application:,,,/Warning_16x16.png"));
    e.Handled = true;
}
vb
Imports DevExpress.Xpf.Editors
Imports System.IO
Imports System.Windows.Media.Imaging
' ...

Private Sub OnCustomPathIcon(ByVal sender As Object, ByVal e As PathIconEventArgs)
    If Not Directory.Exists(e.Path) Then e.Icon = New BitmapImage(New System.Uri("pack://application:,,,/Warning_16x16.png"))
    e.Handled = True
End Sub

The CustomPathIcon event has a higher priority than the PathIconSelector property.

Browse Dialogs

Use the DialogType property to specify the type of the dialog invoked when a user clicks the Browse button or you call the ShowDialog method. The BrowsePathEdit can invoke the following dialogs:

FileOpenThe dialog that allows users to select a file (OpenFileDialog).FileSaveThe dialog that allows users to save a file with the specified name and type (SaveFileDialog).FolderThe dialog that allows users to select a folder (FolderBrowserDialog).

Dialog Events

The BrowsePathEdit raises the following events when a user opens or closes the browse dialog:

DialogOpeningAllows you to cancel the open dialog operation or use the DialogParameters class members to customize the invoked dialog.DialogClosedAllows you to get whether a user selects a file/folder or closes the dialog.

Example

The following code sample specifies the dialog’s initial file name, extension, and folder:

  1. Handle the DialogOpening event.
  2. Specify dialog settings.
  3. Set the e.Handled property to true to apply settings.

xaml
<dxe:BrowsePathEdit DialogType="FileSave" DialogOpening="OnDialogOpening">
csharp
using DevExpress.Xpf.Editors;
using System;
// ...

void OnDialogOpening(object sender, DialogOpeningEventArgs e) {
    e.DialogParameters.FileName = "NewFile";
    e.DialogParameters.DefaultExt = "txt";
    e.DialogParameters.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    e.Handled = true;
}
vb
Imports DevExpress.Xpf.Editors
Imports System
'...

Private Sub OnDialogOpening(ByVal sender As Object, ByVal e As DialogOpeningEventArgs)
    e.DialogParameters.FileName = "NewFile"
    e.DialogParameters.DefaultExt = "txt"
    e.DialogParameters.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    e.Handled = True
End Sub

Drop Files to the Editor

Users can drop files/folders onto the BrowsePathEdit to specify the file/folder path. To enable this functionality, set the editor’s AllowDrop property to true. In this case, the BrowsePathEdit displays the following placeholder text: "Drop a file/folder here".

Use the BaseEdit.NullText property to specify custom placeholder text.

Appearance Customization

Refer to the following help topic for more information: Appearance Customization.

Inheritance

Show 12 items

Object DispatcherObject DependencyObject Visual UIElement FrameworkElement Control BaseEdit TextEditBase TextEdit ButtonEdit BrowsePathEdit

See Also

BrowsePathEdit Members

DevExpress.Xpf.Editors Namespace