了解WPF控件:OpenFileDialog常用属性与用法(十六)

掌握WPF控件:熟练OpenFileDialog常用属性(十六)

  • OpenFileDialog控件在WPF中用于需要用户指定文件路径,为用户提供了一个直观且易用的界面来浏览和选择本地文件系统中的文件。例如,当用户需要打开一个已存在的文本文件时,可以用OpenFileDialog控件来让用户选择文件;当用户需要导入一张图片进行编辑时,也可以用OpenFileDialog控件来选择图片文件。
常用属性 描述
Title 用于获取或设置文件对话框标题
FileName 用于获取或设置用户在文件对话框中选定的文件名的字符串,包括文件的完整路径
FileNames 用于获取对话框中所有选定文件的文件名(当Multiselect属性为True时)
Filter 用于获取或设置筛选器字符串,决定对话框的"文件类型"框中出现的选择内容。
FilterIndex 用于获取或设置文件对话框中当前选定的筛选器的索引。
InitialDirectory 用于获取或设置文件对话框显示的初始目录。
Multiselect 获取或设置一个选项,该选项指示 OpenFileDialog 是否允许用户选择多个文件。
ShowReadOnly 获取或设置一个值,该值指示 OpenFileDialog 是否包含只读复选框。
ReadOnlyChecked 获取或设置一个值,该值指示是否选中 OpenFileDialog 显示的只读复选框。

下面来写个例子

xml 复制代码
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <!--添加两个按钮,分别设置打开图片和打开文本-->
    <Button Grid.Row="0" Grid.Column="0" x:Name="btnOpenImage" Content="打开图片" Background="Blue" Foreground="White" Width="100" Height="40" HorizontalAlignment="Right" VerticalAlignment="Center" Click="btnDialog_Click"/>
    <Button Grid.Row="0" Grid.Column="1"  x:Name="btnOpenTxt" Content="打开文本" Background="Green" Foreground="White" Width="100" Height="40" Margin="10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Click="btnDialog_Click"/>

    <!--添加两个边框 显示默认显示,打开后显示对应效果-->
    <Border Grid.Row="1" Grid.Column="0" Margin="10,10"  BorderBrush="Gray" BorderThickness="1">
        <Image x:Name="myImage"  Stretch="Fill"></Image>
    </Border>
    <Border  Grid.Row="1" Grid.Column="1" Margin="10,10"  BorderBrush="Gray" BorderThickness="1">
        <TextBlock x:Name="myText" Margin="0,10,0,0" FontFamily="Segoe UI"  TextWrapping="Wrap"  FontSize="18"/>
    </Border>
</Grid>
c# 复制代码
namespace WpfOcrApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnDialog_Click(object sender, RoutedEventArgs e)
        {
            Button btnUpload = (Button)sender;
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "All files (*.*)|*.*",
                Title = btnUpload.Name.Equals("btnOpenImage", StringComparison.OrdinalIgnoreCase) ? "选择图片" : "选择TXT文件"
            };
            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                string filePath = openFileDialog.FileName;
                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    switch (btnUpload.Name)
                    {
                        case "btnOpenImage":
                            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            {
                                // 创建一个BitmapImage对象并设置其StreamSource为FileStream  
                                BitmapImage bitmapImage = new BitmapImage();
                                bitmapImage.BeginInit();
                                bitmapImage.StreamSource = stream;
                                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                                bitmapImage.EndInit();
                                // 确保Stream在BitmapImage使用之后被关闭  
                                stream.Close();
                                // 将BitmapImage对象设置为Image控件的Source  
                                myImage.Source = bitmapImage;
                            }

                            break;
                        case "btnOpenTxt":
                            using (StreamReader reader = new StreamReader(filePath))
                            {
                                // 读取文件内容
                                string content = reader.ReadToEnd();
                                reader.Close();
                                myText.Text = content;
                            }

                            break;
                    }

                }
            }
        }
    }
}

上述代码运行效果如下:

相关推荐
Aevget3 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(一)
c#·wpf·界面控件·devexpress·ui开发
The Sheep 20232 天前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴2 天前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty0072 天前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU2 天前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机2 天前
wpf之Interaction.Triggers
c#·wpf
是木子啦2 天前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20232 天前
wpf 命令理解
wpf
布伦鸽2 天前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS3 天前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe