如何使用按钮单击事件关闭主窗口类中的对话框

时间:2016-11-23 06:48:50

标签: c# c#-4.0

我的代码

public partial class MainWindow : Window
    {
        string GlobalFilePath = string.Empty;
        public MainWindow()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, MenuItem_Click));
            //CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, MenuItem_Click_save, CanSave));
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, MenuItem_Click_1));

        // Instantiate window
        // Instantiate window
        Window dialogBox = new Window();
        var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
        stackPanel.Children.Add(new Label { Content = "Select File Path" });

        TextBox textbox_path = new TextBox();         
        stackPanel.Children.Add(textbox_path);        

        Button button = new Button();
        button.Height = 20;
        button.Content = "Browse";
        button.Click += new RoutedEventHandler(this.func1_Click);
        stackPanel.Children.Add(button);

        Button button1 = new Button();
        button1.Height = 20;
        button1.Content = "Save";
        button1.Click += new RoutedEventHandler(this.func2_Click);
        stackPanel.Children.Add(button1);
        dialogBox.Content = stackPanel;
        // Show window modally
        // NOTE: Returns only when window is closed

        bool? dialogResult =  dialogBox.ShowDialog();
    }

     private void func1_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
        System.Windows.Forms.DialogResult result = fd.ShowDialog();
        GlobalFilePath = fd.Selectedpath;           
    }

    private void func2_Click(object sender, RoutedEventArgs e)
    {
        dialogBox.close();
    }

0 个答案:

没有答案