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

                }
            }
        }
    }
}

上述代码运行效果如下:

相关推荐
贪玩的小松鼠18 小时前
WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核
wpf
界面开发小八哥18 小时前
界面控件DevExpress WPF v26.1新版亮点 - TreeView、Spreadsheet控件功能升级
.net·wpf·界面控件·devexpress·ui开发
斯文的八宝粥2 天前
WPF企业内训全程实录(下)
大数据·hadoop·wpf
精明的身影4 天前
深入WPF -- Dispatcher(补)
wpf
云中飞鸿5 天前
该如何进行WPF界面设计
wpf
云中飞鸿6 天前
WPF分哪几块
wpf
newbe365246 天前
我们如何使用 impeccable 优化前端界面设计与实现稳定性
前端·人工智能·分布式·github·aigc·wpf
Chris _data24 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
布吉岛的石头24 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet24 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf