XAF Folder Browser Dialog [Custom Property Editor]

Paul Usher's Blog
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




Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.