首页 > 系统相关 > Windows and Dialogs 窗体 对话框

Windows and Dialogs 窗体 对话框

时间:2023-01-20 12:00:27浏览次数:50  
标签:dlg string 对话框 Windows System 窗体 dialog using public

 

 

 

<Window x:Class="WpfTestBlankApp.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTestBlankApp.Models"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="600" Width="500" 
         xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
 xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework">
    <StackPanel>
        <Button x:Name="openFileDialog" Click="openFileDialog_Click" Content="打开文件"></Button>
        <Button x:Name="openDialog" Click="openDialog_Click" Content="打开模式对话框"></Button>
        <Button x:Name="openModelessDialog" Click="openModelessDialog_Click" Content="打开对话框"></Button>
    </StackPanel>
</Window>
using Microsoft.Win32;
using Prism.Regions;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfTestBlankApp.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string currentPlayer;
        Button[] cells;
        int moveNumber;
        string reporter;
        public MainWindow(IRegionManager regionManager)
        {
            InitializeComponent();
            reportColor = Colors.Red;
            reportFolder = @"E:\电子书\WPF";
        }
        string filename;
        private void openFileDialog_Click(object sender, RoutedEventArgs e)
        {//文件对话框
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = filename;
            if (dlg.ShowDialog() == true)
            {
                filename = dlg.FileName;
            }
        }
        Color reportColor;
        string reportFolder;
        private void openDialog_Click(object sender, RoutedEventArgs e)
        {//打开模式对话框
         //1. 使用初始值创建并初始化对话框
            SettingsDialog dialog = new SettingsDialog();
            dialog.Owner = this;
            dialog.ReportColor = reportColor;
            dialog.ReportFolder = reportFolder;
            //2.显示对话框,让用户选择新的、经过验证的值
            if (dialog.ShowDialog() == true)
            { //3.使用用户设置的新值
                reportColor = dialog.ReportColor;
                reportFolder = dialog.ReportFolder;
            }
        }

        private void openModelessDialog_Click(object sender, RoutedEventArgs e)
        {//打开无模式对话框
            // Initialize the dialog
            SettingsDialog dlg = new SettingsDialog();
            dlg.Owner = this;
            dlg.ReportColor = reportColor;
            dlg.ReportFolder = reportFolder;
            dlg.Reporter = reporter;
            // Listen for the Apply button and show the dialog modelessly
            dlg.Apply += dlg_Apply;
            dlg.Show();
        }

        void dlg_Apply(object sender, EventArgs e)
        {
            // Pull the dialog out of the event args and apply the new settings
            SettingsDialog dlg = (SettingsDialog)sender;
            reportColor = dlg.ReportColor;
            reportFolder = dlg.ReportFolder;
            reporter = dlg.Reporter;
            // Do something with the dialog properties
        }
    }
}
<Window x:Class="WpfTestBlankApp.Views.SettingsDialog"
        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:WpfTestBlankApp.Views"
        xmlns:model="clr-namespace:WpfTestBlankApp.Models"
        mc:Ignorable="d"
        Title="SettingsDialog" Height="450" Width="800"
        ResizeMode="CanResizeWithGrip" WindowStartupLocation="CenterOwner"
        FocusManager.FocusedElement="{Binding ElementName=reportFolderTextBox}"
        ShowInTaskbar="False">
    <Window.Resources>
        <Style TargetType="Label">
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="10"/>
            <Setter Property="Padding" Value="5,2"/>
        </Style>

    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <SolidColorBrush x:Key="reportBrush" Color="{Binding ReportColor}" />
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition MinWidth="200" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <!-- 1st row: report folder setting -->
        <Label Grid.Row="0" Grid.Column="0"
 Target="{Binding ElementName=reportFolderTextBox}">Report _Folder</Label>
        <TextBox Grid.Row="0" Grid.Column="1" Name="reportFolderTextBox"
                 ToolTip="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent }">
            <TextBox.Text>
                <Binding Path="ReportFolder">
                    <Binding.ValidationRules>
                        <model:FolderMustExist/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Button Grid.Row="0" Grid.Column="2" Name="folderBrowseButton">...</Button>
        <!-- 2nd row: report color setting -->
        <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
 Name="reportColorButton">
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="15" Height="15" SnapsToDevicePixels="True"
 Fill="{StaticResource reportBrush}" />
                <AccessText Text="Report _Color..." Margin="10,0,0,0" />
            </StackPanel>
        </Button>
        <!-- 3rd row: buttons -->
        <Label Grid.Row="2" Target="{Binding ElementName=reporterTextBox}">_Reporter</Label>
        <TextBox  Grid.Row="2" Grid.Column="1" x:Name="reporterTextBox" 
                 ToolTip="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}">
            <TextBox.Text>
                <Binding Path="Reporter" >
                    <Binding.ValidationRules >
                        <model:NonZeroLength ValidatesOnTargetUpdated="True"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

        <StackPanel Grid.Row="4" Grid.ColumnSpan="3" Orientation="Horizontal"
 HorizontalAlignment="Right" VerticalAlignment="Bottom">
            <Button Name="applyButton" Width="72" Click="applyButton_Click">Apply</Button>
            <Button Name="okButton" IsDefault="True" Width="72" Click="okButton_Click">OK</Button>
            <Button Name="cancelButton" IsCancel="True"  Width="72" >Cancel</Button>
        </StackPanel>
    </Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using WpfTestBlankApp.Models;

namespace WpfTestBlankApp.Views
{
    /// <summary>
    /// SettingsDialog.xaml 的交互逻辑
    /// </summary>
    public partial class SettingsDialog : Window
    {
        public SettingsDialog()
        {
            InitializeComponent();
            //reportFolderTextBox.Text = ReportFolder;
            DataContext = data;
        }
        DialogData data = new DialogData();
        public event EventHandler Apply;
        public Color ReportColor
        {
            get { return data.ReportColor; }
            set { data.ReportColor = value; }
        }
        public string ReportFolder
        {
            get { return data.ReportFolder; }
            set { data.ReportFolder = value; }
        }
        public string Reporter
        {
            get { return data.Reporter; }
            set { data.Reporter = value; }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            // Validate all controls
            if (ValidateBindings(this))
            {
                DialogResult = true;
            }
        }
        public static bool ValidateBindings(DependencyObject parent)
        {
            // Validate all the bindings on the parent
            bool valid = true;
            LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
            while (localValues.MoveNext())
            {
                LocalValueEntry entry = localValues.Current;
                if (BindingOperations.IsDataBound(parent, entry.Property))
                {
                    Binding binding = BindingOperations.GetBinding(parent, entry.Property);
                    foreach (ValidationRule rule in binding.ValidationRules)
                    {
                        ValidationResult result =
                        rule.Validate(parent.GetValue(entry.Property), null);
                        if (!result.IsValid)
                        {
                            BindingExpression expression =
                            BindingOperations.GetBindingExpression(parent, entry.Property);
                            Validation.MarkInvalid(expression,
                            new ValidationError(rule, expression, result.ErrorContent, null));
                            valid = false;
                        }
                    }
                }
            }
            // Validate all the bindings on the children
            for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                if (!ValidateBindings(child)) { valid = false; }
            }
            return valid;
        }

        private void applyButton_Click(object sender, RoutedEventArgs e)
        {//非模式窗口
            if (Apply != null)
            {
                Apply(this, EventArgs.Empty);
            }
        }
    }
}
using System.ComponentModel;
using System.Windows.Media;

namespace WpfTestBlankApp.Models
{
    public class DialogData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        void Notify(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
        Color reportColor;
        public Color ReportColor
        {
            get { return reportColor; }
            set { reportColor = value; Notify("ReportColor"); }
        }
        string reportFolder;
        public string ReportFolder
        {
            get { return reportFolder; }
            set { reportFolder = value; Notify("ReportFolder"); }
        }
        string reporter;
        public string Reporter
        {
            get { return reporter; }
            set { reporter = value; Notify(nameof(Reporter)); }
        }

    }
}

 

标签:dlg,string,对话框,Windows,System,窗体,dialog,using,public
From: https://www.cnblogs.com/friend/p/17062635.html

相关文章

  • Windows下udp广播【C++】
     最近学习了下Windows下的Socket使用,在这里记录一下。 前置准备 在使用api前,需要做一些必要的准备。头文件包含  //包括Winsock2头文件使用WinsockAPI,......
  • 第五十章 使用 ^SystemPerformance 监视性能 - Microsoft Windows 平台的 InterSystem
    第五十章使用^SystemPerformance监视性能-MicrosoftWindows平台的InterSystemsIRIS性能数据报告MicrosoftWindows平台的IRIS性能数据报告%SS-使用ALL......
  • windows
    将软件启动添加至右键快捷方式--powershell#powershell 所在目录为 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe1.windows+r2.键入regedit(打开注......
  • 记一次配置Windows终端terminal
    立即安装Windows终端terminalGit发布页面发布地址https://github.com/microsoft/terminal/releases应用商店安装地址https://aka.ms/terminalwingetwingetinstall......
  • Windows Server上部署IoTdb 集群
    本文是参考官方的IoTDB集群版(1.0.0)的安装及启动教程:https://iotdb.apache.org/zh/UserGuide/V1.0.x/Cluster/Cluster-Setup.html,在WindowsServer2019上部署集群的实践......
  • 通过WSL2在Windows11环境下运行Xilinx ISE 14.7
    引言最近我开始学习FPGA,但是软件配置上就折腾了好久,所以通过这篇文章记录一下Win11下ISE的安装流程。开始我按照入门教程安好了Vivado打算开始愉快的学习,结果发现...我买......
  • 如何解决安装Windows 11/10时找不到磁盘驱动器
     001、利用u盘对华硕笔记本安装系统时出现如下问题  002下载IntelRapidStorageTechnology(IRST),下载链接对其进行解压:  003、将其拷贝至U盘介质中 ......
  • 解决:windows下php curl https时 SSL operation failed with code 1. OpenSSL Error me
    下载证书curl-ExtractCACertsfromMozillahttps://curl.se/docs/caextract.html 配置php.ini,把路径放上去  搞定收工......
  • windows电脑上如何投屏苹果手机
    软件下载下载软件点击这里首次手机在电脑上投屏下载爱思的手机投屏软件,如下图:软件下载完之后,需要安装驱动如下图:安装完之后点击开始投屏,如下图:安装失败......
  • x64 windows 8 下无法安装QQ2012
    安装好windows8后,应用商店中居然有QQ,很是激动,于是下载安装.安装完成后发现功能有限,于是上QQ官方网站下载QQ2012版,安装完成后,运行提示错误,无法运行.具体错误内容忘了,......