首页 > 其他分享 >WPF OpenFielDialog Filter InitialiaDirectory Title

WPF OpenFielDialog Filter InitialiaDirectory Title

时间:2024-07-03 19:20:38浏览次数:1  
标签:dlg Title Windows System Filter using InitialiaDirectory WPF MainWindow

//xaml
<Window x:Class="WpfApp189.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp189"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="Open Image" FontSize="20" Click="OnOpenImage"
                HorizontalAlignment="Center" Margin="4"/>
        <Image Grid.Row="1" x:Name="img" Stretch="Uniform"/>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Microsoft.Win32;

namespace WpfApp189
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnOpenImage(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog()
            {
                Filter = "Image files|*.png;*.jpg;*.gif;*.bmp|All Files|*.*",
                Title = "Select image to open",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            };
            if(dlg.ShowDialog()==true)
            {
                try
                {
                    var bmp = new BitmapImage(new Uri(dlg.FileName));
                    img.Source = bmp;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Open Image");
                }
            }
        }
    }
}

//cs

 

标签:dlg,Title,Windows,System,Filter,using,InitialiaDirectory,WPF,MainWindow
From: https://www.cnblogs.com/Fred1987/p/18282410

相关文章

  • WPF Prism PubSubEvent(订阅)
    Prism提供了事件聚合器(EventAggregator)来实现事件的订阅和发布,允许模块之间进行松耦合的通信。主要作用:解耦合:通过事件订阅和发布,模块之间可以实现解耦合,避免直接依赖于彼此的实现细节。示例用法:定义事件类:publicclassMessageEvent:PubSubEvent<string>{}订......
  • WPF进度条中间写百分比数字
    我发现很多同学把思维固话了,通常我们需要实现的进度条是我在网上看到好多例子,但是都没有我的简单,他们不是重写ProcessBar就是使用模板,可以将TextBlock提取出来啊,灵活一点单独绑定然后一句代码Panel.ZIndex="1"就搞定了<StackPanel><ButtonContent="执行耗时......
  • 使用Filter接口编写过滤器解决post乱码
    在使用tomcat9以及之前的版本,request-character-encoding和response-character-encoding使用的字符编码默认不是utf-8,所以导致前端发送到后台的中文乱码.如果使用的是tomcat10以及之后的版本,在apache-tomcat-10.1.25\conf\web.xml已设置好默认的字符集编码为utf-8,如果所示:......
  • DevExpress WPF中文教程:Grid - 如何显示摘要(设计时)?
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • WPF Image scalertransform translatetransform mvvm,custom delegate command Comman
    //xaml<Windowx:Class="WpfApp187.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • wpf关于Resource,Style的定义与引用,滑动按钮
    当我们使用wpf框架去搭建自己的程序时,一般都会重写wpf原生的一些样式,以达到软件风格的统一于美化,以下介绍一下常见的几种添加Style的方式我们以一个滑动按钮为例1.对当前控件的样式进行更改<ToggleButton><ToggleButton.Style><StyleTarg......
  • WPF 分隔栏分割窗体简单测试
    XAML:<Windowx:Class="WpfApp3.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micro......
  • WPF 中 StackPanel 控件的可视化 Visibility.Collapsed 控件元素会自动前移
    XAML:<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinitionHeight="Auto"/></Grid.RowDefinitions><StackPanelGrid.Row="0"><TextBoxx:Name="txb_001&quo......
  • 开发界的良心评测文章,告诉你WPF 与 WinForms 对比:性能、开发难度、特点及操作系统支持
    引言WindowsPresentationFoundation(WPF)和WindowsForms(WinForms)是微软提供的两种用于构建Windows桌面应用程序的框架。尽管它们都有助于开发高效的桌面应用程序,但在性能、开发难度、特点和操作系统支持方面存在显著差异。本文将详细比较WPF和WinForms,以帮助开发者选择......
  • 基于WPF+Halcon开发的机器视觉框架,参考easyvision开发
    基于WPF+Halcon开发的机器视觉框架,参考easyvision开发50多个模块,WPF+Halcon开发,是非常不错的学习框架,需要什么功能可以自己添加开发,插件式开发非常方便,代码开源可二次开发。整套源码,可学习参考,也可略做修改,用在自己的项目上。基于WPF+Halcon开发的机器视觉框架运行界面......