XAF Folder Browser Dialog [Custom Property Editor]

13 April 2013

 

One of the (many) powerful features of XAF is the ability to extend. I recently had a client wanting to provide a simple dialog to let the user choose a folder. There is a file selection function already built into XAF, but since we only wanted a folder it would not suffice.

To achieve what was needed I had to create a CustomPropertyEditor and hook into the FolderBrowserDialog available in the System.Windows.Forms library.

Simply create a new class in your Windows module (if you have an existing folder for Editors, it’s a good place to store it), you can paste the code from below (just remember to update the namespace Smile)

using System;
using System.Collections.Generic;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Editors;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

namespace MM.Module.Win.Editors {
​    [PropertyEditor(typeof(string), "FolderBrowseEditor", false)]
    public class FolderBrowseEditor : DXPropertyEditor {
        public FolderBrowseEditor(Type objectType, IModelMemberViewItem model)
            : base(objectType, model) { }
        private void buttonEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog()) {
                dialog.Description = "Select folder...";
                if (dialog.ShowDialog() != DialogResult.Cancel) {
                    ((ButtonEdit)sender).Text = dialog.SelectedPath;
                }
            }
        }
        protected override object CreateControlCore() {
            return new ButtonEdit();
        }
        protected override RepositoryItem CreateRepositoryItem() {
            return new RepositoryItemButtonEdit();
        }
        protected override void SetupRepositoryItem(RepositoryItem item) {
            base.SetupRepositoryItem(item);
            ((RepositoryItemButtonEdit)item).ButtonClick += buttonEdit_ButtonClick;
        }
        protected override void SetRepositoryItemReadOnly(RepositoryItem item, bool readOnly) {
            base.SetRepositoryItemReadOnly(item, readOnly);
            ((RepositoryItemButtonEdit)item).Buttons[0].Enabled = !readOnly;
        }
    }
}


Build your project, and then inside the model you will be able to choose YourProject.Module.Win.Editors.FolderBrowserEditor as a property type.  Run your application and you will see a nice XtraEditors.ButtonEdit control rendered with a […] button, clicking on the button will present you with a standard OS folder browser, if you select a folder, the result will be returned to the control and your data updated !

x1


x2

x3


For more information on custom property editors please see http://documentation.devexpress.com/#Xaf/CustomDocument2679




7 comment(s)
James Zhong

Thanks for sharing this tip :)

15 April, 2013
Avril Craighead

Is it possible to modify this so that you can have a hyperlink for the string and it opens up the directory once clicked upon?

15 April, 2013
Paul Usher (DevExpress)

Yes; although an alternate would be,

1. include an additional button on the button edit control

2. set the image to a small open folder

3. on the click event, add Process.Start(PropertyValue);

You now have a quick way of getting to the folder.

15 April, 2013
Avril Craighead

Thanks for the tip

16 April, 2013
Willem de Vries

Thanks for sharing, Paul!

23 April, 2013
Apostolis Bekiaris (DevExpress)

Nice one Paul! Your implementation added to our next version (12.2.8.9) of our community project eXpandFramework.com.

24 April, 2013
Nate Laff

You have a flaw in the implementation. When this property editor is displayed in a detail view, the object is set to modified.

25 June, 2013

Please login or register to post comments.