[Update to V4] WPF: more productive openfiledialog, savefiledialog, and folderbrowserdialog

Source: Internet
Author: User

These three dialogs are believed to be well understood by traditional desktop developers (names may vary, but functions are similar ). In the WPF environment, these three dialogs are generally used as follows:

The open and save dialog box uses the openfiledialog and savefiledialog types in the Microsoft. Win32 namespace, And the folderbrowserdialog type in Windows forms can be used in the Open folder dialog box.

 

The three controls I wrote: openfilecontrol, savefilecontrol, and openfoldercontrol. In fact, these three types of packages are more productive because they have the following features:

    1. All are control types and can be defined and used in XAML.
    2. Filter and button text can be set in XAML, which facilitates multi-language.
    3. Supports binding WPF commands (icommand type ).
    4. The file path is a dependency attribute that facilitates data binding and mvvm.
    5. You can set the internal textbox and button styles.
    6. Drag and Drop operations are supported.

 

During execution, the three control types are inherited from the commondialogcontrol Control, which describes some attributes:

Commandtext: the text of the Browse button.

Filter: the filter attribute of the dialog box.

Path: The selected path.

Command: the WPF command to be bound.

Buttonstyle and textboxstyle: the style of the internal textbox and button.

(All these attributes are WPF dependency attributes)

 

For example:

<! -- LOC is the local namespace of savefilecontrol -->

<Loc:SavefilecontrolCommand="Save"Commandtext="Save (_ s )..."/>

 

At this time, savefilecontrol is bound to the applicationcommands. Save command, and commandtext sets the content of the button. Note that you can use underscores to specify the shortcut key. Press Ctrl + S to trigger the application.ProgramSave command. The save dialog box is displayed. At the same time, pressing Alt + S will trigger the button clicking event, so the Save dialog box will also be displayed.

 

Using these controls is very convenient in mvvm mode, as shown below:Code:

<! -- LOC is the local namespace of savefilecontrol -->

<! -- The property of myfile is data in viewmodel -->

<Loc:SavefilecontrolCommand="Save"Commandtext="Save"Path= "{BindingMyfile}"/>

Define controls in the view and bind them directly to the properties of the viewmodel.

 

The default style of the control is also OK:

Of course, you can customize the style through the textboxstyle and buttonstyle attributes:

 

Commondialogcontrol also contains two events:

Opened and shown. When the shown event is displayed in the dialog box, the parameter of the shown event is commondialogshowneventargs. It has a result attribute that indicates that the user selects "OK ".

The opened event will occur after the dialog box is displayed and the user selects OK. The path attribute of the control will be updated. Note that commondialogcontrol does not define the pathchanged event. The main reason is that the path attribute is already a dependency attribute, but you can add it as needed.

 

Finally, it is a method of commondialogcontrol: browse. After browse is called, the dialog box is displayed.

 

Source codeThere is a sample program. You can use the sample program to review the above content. Everything is very simple.

 

Update:

Version 2

You can add the defaultextension attribute to the savefilecontrol control to set the defaultext attribute of the savefiledialog type.

Version 3

Fixed the tab problem.

Version 4

Fixed the problem where the previous path is not displayed in the dialog box.

Delete the defaultextension attribute (refer to: Is defaultext and addextension useful in WPF: filedialog type ?).

 

Download programs and source code

V4

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.

Source code environment: Microsoft Visual Studio express 2012 for Windows Desktop

Note: The target platform of the source code project is. NET Framework 3.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.