了解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;
                    }

                }
            }
        }
    }
}

上述代码运行效果如下:

相关推荐
明耀15 小时前
WPF DataGrid 默认显示行号
wpf
lph197219 小时前
wpf的converter
wpf
fyifei055819 小时前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友19 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf202320 小时前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白20 小时前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans2 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf
code bean3 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf