当我单击 WPF (.NET Core) 中第二个窗口的“确定”按钮时,禁用 MainWindow 按钮并返回到 MainWindow 的初始状态

尼克斯普

我在 WPF 应用程序中有两个 Windows。第一个是 MainWindow 而第二个是用数据填充 MainWindow 的 SecondaryWindow。

简而言之,我的 MainWindow 中有一个“加载”按钮,该按钮仅在用户浏览本地文件时才被禁用和启用。当“加载”被启用时,用户被要求填写一些凭据以将数据加载到 SQL 服务器中的表中。当用户在第二个窗口中单击“确定”时,我希望我的 MainWindow 返回其初始状态。

MainWindow.xaml.cs 文件

using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
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 System.Data.SqlClient;
using System.Diagnostics;
using System.Data;
using System.IO;

namespace TestEnvironment
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    
    public static class StringExtensions
    {
        public static string Args(this string str, params object[] args)
        {
            return String.Format(str, args);
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // Functions - Methods

        public DataTable ConvertToDataTable(string filePath)
        {
            DataTable tbl = new DataTable();

            // Take the first 10 lines
            var lines = File.ReadLines(filePath).Take(10);

            // Split each line and create an integer sequence where each value 
            // is the number of the splitted elements
            // then get the max value present in this sequence
            var max = lines.Select(x => x.Split('\t').Length).Max();

            // First line contains headers
            string[] headers = lines.First().Split('\t');

            // Now create the table with the max number of columns present
            for (int col = 0; col < max; col++)
                tbl.Columns.Add(headers[col], typeof(string));

            //Use the Rows.Add method that accepts an object array
            foreach (string line in lines.Skip(1))
            {

                tbl.Rows.Add(line.Split('\t'));

            }

            //UTF-8 encoding

            /*var utf8 = Encoding.UTF8;

            foreach (string line in lines.Skip(1))
            {
                IEnumerable<string> utf8Values = line.Split('\t')
                    .Select(s => utf8.GetString(Encoding.Convert(Encoding.ASCII, utf8,
                        Encoding.ASCII.GetBytes(s))));
                tbl.Rows.Add(utf8Values);
            }*/

            /*foreach (DataRow dr in tbl.Rows)
            {

                Debug.WriteLine(dr["Nationality"]);

                string s = dr["Nationality"].ToString();

                byte[] bytes = Encoding.Default.GetBytes(s);
                dr["Nationality"] = Encoding.UTF8.GetString(bytes);

                Debug.WriteLine(dr["Nationality"]);
            }*/

            return tbl;
        }

        public static void WriteDataToFile(DataTable submittedDataTable, string submittedFilePath)
        {
            int i = 0;
            StreamWriter sw = null;

            sw = new StreamWriter(submittedFilePath, false);

            for (i = 0; i < submittedDataTable.Columns.Count - 1; i++)
            {

                sw.Write(submittedDataTable.Columns[i].ColumnName + ";");

            }
            sw.Write(submittedDataTable.Columns[i].ColumnName);
            sw.WriteLine();

            foreach (DataRow row in submittedDataTable.Rows)
            {
                object[] array = row.ItemArray;

                for (i = 0; i < array.Length - 1; i++)
                {
                    sw.Write(array[i].ToString() + ";");
                }
                sw.Write(array[i].ToString());
                sw.WriteLine();

            }

            sw.Close();
        }

        public static void SubmitData(string connectionString, string tablenametext)
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();
                using (var command = sqlConnection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "createselectedtableinDB";
                    command.Parameters.AddWithValue("@TableName", tablenametext);
                    command.ExecuteNonQuery();
                }
            }
        }

        public static string CreateConnectionString(string servernametext, string databasenametext)
        {
            string connectionstring = "Server={0};Database={1};Integrated Security=SSPI".Args(servernametext, databasenametext);

            return connectionstring;
        }

        // Object Interactions

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {   

            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();

            // Launch OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = openFileDlg.ShowDialog();
            // Get the selected file name and display in a TextBox.
            // Load content of file in a TextBlock
            if (result == true)
            {
                FileNameTextBox.Text = openFileDlg.FileName;
                TextBlock1.Text = "Created on: " + File.GetCreationTime(openFileDlg.FileName).ToString() +"\n";
                
                Debug.WriteLine(File.GetCreationTime(openFileDlg.FileName).ToString());
                
                var datatablematrix = ConvertToDataTable(filePath: openFileDlg.FileName);

                /*Debug.WriteLine(datatablematrix);

                WriteDataToFile(datatablematrix, @"C:\Users\spano\Desktop\ApplicationOption2\testdummy.txt");*/

                grid.DataContext = datatablematrix.DefaultView;

            }

            // Set filter for file extension and default file extension  
            openFileDlg.DefaultExt = ".txt";
            openFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            Debug.WriteLine("Txt imported");

            // Set initial directory    
            openFileDlg.InitialDirectory = @"C:\Documents\";

            // Multiple selection with all file types    
            openFileDlg.Multiselect = true;

            BrowseButton.IsEnabled = true;
            LoadButton.IsEnabled = true;

            Debug.WriteLine("End!");

        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            TableNamePopupwindow popup = new TableNamePopupwindow();
            //ShowDialog means you can't focus the parent window, only the popup
            popup.ShowDialog(); //execution will block here in this method until the popup closes
            
            string resultTable = popup.TableNameValue;
            string resultServer = popup.ServerNameValue;
            string resultDatabase = popup.DatabaseNameValue;

            var connectionString = CreateConnectionString(resultServer, resultDatabase);

            SubmitData(connectionString, resultTable);

        }

        private void PowerButton_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void TabablzControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

TableNamePopupwindow.xaml.cs

using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
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 System.Data.SqlClient;
using System.Diagnostics;
using System.Data;
using System.IO;

namespace TestEnvironment
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class TableNamePopupwindow : Window

    {
        public string TableNameValue

        {
           get
            {
                if (TableName == null) return string.Empty;

                return TableName.Text;
            }
        }

        public string ServerNameValue

        {
            get
            {
                if (ServerName == null) return string.Empty;

                return ServerName.Text;
            }
        }

        public string DatabaseNameValue

        {
            get
            {
                if (DatabaseName == null) return string.Empty;

                return DatabaseName.Text;
            }
        }

        public TableNamePopupwindow()
        {
            InitializeComponent();
        }

        private void OnOk_Click(object sender, RoutedEventArgs e)
        {   
            //code I found here: https://stackoverflow.com/questions/46089017/wpf-passing-text-from-one-window-to-another-window
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            mainWindow.LoadButton.IsEnabled = false;
            this.Close();
        }
    }
}

正如您在上面看到的 OnOK 单击我希望重新禁用“LoadButton”。

MainWindow 屏幕截图(用户打开应用程序时的初始状态)

在此处输入图片说明

MainWindow 截图(用户浏览文件)

在此处输入图片说明

SecondWindow 的屏幕截图(当用户单击“加载”按钮时)

在此处输入图片说明

我想要的是当用户单击“确定”按钮关闭 MainWindow 的第二个窗口以返回其初始状态时(附上第一个屏幕截图)。根据我在this SO question上找到的第二个窗口的代码,我设法以初始状态返回,但每次单击“确定”按钮时也会打开一个新的 MainWindow 。

毫米8

与其MainWindowOnOk_Click处理程序中打开一个新实例,不如修改已经存在和打开的实例。

您需要以某种方式获得对它的引用。例如,您可以TableNamePopupwindow在打开引用时注入引用:

private readonly MainWindow _mainWindow;

public TableNamePopupwindow(MainWindow mainWindow)
{
    InitializeComponent();
    _mainWindow = mainWindow;
}

private void OnOk_Click(object sender, RoutedEventArgs e)
{
    _mainWindow.LoadButton.IsEnabled = false;
    this.Close();
}

主窗口:

TableNamePopupwindow popup = new TableNamePopupwindow(this);
popup.ShowDialog();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章