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

发布于:2024-06-30 ⋅ 阅读:(17) ⋅ 点赞:(0)

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

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

下面来写个例子

<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>


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

                }
            }
        }
    }
}

上述代码运行效果如下:
选择文件

展现数据效果