zoukankan      html  css  js  c++  java
  • 文件对话框WPF(5)----文件浏览对话框

    废话就不多说了,开始。。。

        WPF中文件浏览对话框的实现可以利用Windows API Code Pack,它是一个用于访问Windows Vista/7 特性的托管代码函数库,但并没有包含在.NET 4.0中。

        该代码包的特性如下所示:

        

        

    • 支撑Windows Shell命名空间对象,包含新的Windows 7资源库(Libraries)、固定名称文件夹和非文件系统容器。
    • Windows Vista和Windows 7任务对话框(Task Dialogs)。
    • 支撑WPF和Windows Forms的Windows 7资源管理器浏览器控件(Explorer Browser Control)。
    • 支撑Shell的属性系统。
    • 用于Windows 7任务栏Jumplists、Icon Overlay和Progress Bar的帮助程序。
    • 支撑Windows Vista和Windows 7的通用文件对话框,并包含了自定义文件对话框控件。
    • 支撑Direct3D 11.0和DXGI 1.0/1.1的API。
    • 传感器平台(Sensor Platform)API
    • 扩展的语言服务(Extended Linguistic Services)API。

        

        1:代码包下载之后,解压,将其中的Microsoft.WindowsAPICodePack.dll 和Microsoft.WindowsAPICodePack.Shell.dll拷贝至工程中。然后Reference-->Add将其添加至Project中的References。

        文件和对话框

        

        2:代码编写时,将其导入命名空间:

    using Microsoft.WindowsAPICodePack.Shell;
    using Microsoft.WindowsAPICodePack.Dialogs;

        3:前台xmal代码如下:

    <Window x:Class="WpfFileExploerDialog.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="148" Width="434" Background="#E609072F">
        <Grid Name="Grid1">
            <TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBoxFilePath" VerticalAlignment="Top" Width="347" />
            <Button Content="..." Click="ButtonFileSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="buttonFileDialog" VerticalAlignment="Top" Width="25" />
        </Grid>
    </Window>

        4:后台xmal.cs代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    using System.ComponentModel;  
    using Microsoft.WindowsAPICodePack.Shell;
    using Microsoft.WindowsAPICodePack.Dialogs;
    
    namespace WpfFileExploerDialog
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged  
        {
            public MainWindow()
            {
                InitializeComponent();
                Grid1.DataContext = this; 
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void NotifyPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }
    
            private string _Value2;
    
            public string TextBoxValue
            {
                get { return _Value2; }
                set
                {
                    if (value != _Value2)
                    {
                        _Value2 = value;
                        NotifyPropertyChanged("TextBoxValue");
                    }
                }
            }  
    
            private void ButtonFileSelect(object sender, RoutedEventArgs e)
            {
                ShellContainer selectedFolder = null;
                selectedFolder = KnownFolders.Computer as ShellContainer;
                CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
                commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
                commonOpenFileDialog.EnsureReadOnly = true;
    
    
                if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    TextBoxValue = commonOpenFileDialog.FileName;
                }
            }
        }
    }
        每日一道理
    如果说生命是一座庄严的城堡,如果说生命是一株苍茂的大树,如果说生命是一只飞翔的海鸟。那么,信念就是那穹顶的梁柱,就是那深扎的树根,就是那扇动的翅膀。没有信念,生命的动力便荡然无存;没有信念,生命的美丽便杳然西去。(划线处可以换其他词语)

        5:程序运行结果如下:

        文件和对话框

        

        

        另外,还可以将文件浏览窗口直接定位到固定的文件夹,并且添加想要的文件过滤器,例如上面的代码就是将其定位到SampleVideos文件夹:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    using System.ComponentModel;  
    using Microsoft.WindowsAPICodePack.Shell;
    using Microsoft.WindowsAPICodePack.Dialogs;
    
    namespace WpfFileExploerDialog
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged  
        {
            public MainWindow()
            {
                InitializeComponent();
                Grid1.DataContext = this; 
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void NotifyPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }
    
            private string _Value2;
    
            public string TextBoxValue
            {
                get { return _Value2; }
                set
                {
                    if (value != _Value2)
                    {
                        _Value2 = value;
                        NotifyPropertyChanged("TextBoxValue");
                    }
                }
            }  
    
            private void ButtonFileSelect(object sender, RoutedEventArgs e)
            {
                ShellContainer selectedFolder = null;
    
                //文件夹定位至SampleVideos
                selectedFolder = KnownFolders.SampleVideos as ShellContainer;
                CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
                commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
                commonOpenFileDialog.EnsureReadOnly = true;
    
                //设置文件过滤
                commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
                commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
                commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
                commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MKV Files", "*.mkv"));
    
                if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    TextBoxValue = commonOpenFileDialog.FileName;
                }
            }
        }
    }

        

    文章结束给大家分享下程序员的一些笑话语录: 看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。

    --------------------------------- 原创文章 By
    文件和对话框
    ---------------------------------

  • 相关阅读:
    h5-7
    h5-6
    h5-5
    h5-4
    h5-3
    h5-2
    return
    字符串的常用操作
    字符串中的转义字符
    字典的统计,合并,清空操作
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3153455.html
Copyright © 2011-2022 走看看