bovender framework
C# framework that implements MVVM and more
FileDialogActionBase.cs
1 /* FileDialogActionBase.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using Bovender.Mvvm.Messaging;
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23 using System.Windows.Forms;
24 
25 namespace Bovender.Mvvm.Actions
26 {
32  {
33  #region Public properties
34 
47  public string Filter { get; set; }
48 
49  #endregion
50 
51  #region Abstract methods
52 
53  protected abstract FileDialog GetDialog(
54  string defaultString,
55  string filter);
56 
57  #endregion
58 
59  #region Overrides
60 
61  protected override string GetDialogResult(
62  string defaultString,
63  string filter)
64  {
65  FileDialog dlg = GetDialog(defaultString, filter);
66  dlg.Title = this.Caption;
67  if (String.IsNullOrEmpty(dlg.Filter))
68  {
69  if (!String.IsNullOrEmpty(Filter))
70  {
71  dlg.Filter = Filter;
72  }
73  else
74  {
75  dlg.Filter = _messageContent.Filter;
76  }
77  }
78  if (dlg.ShowDialog(new Win32Window()) == DialogResult.OK)
79  {
80  return dlg.FileName;
81  }
82  else
83  {
84  return String.Empty;
85  }
86  }
87 
88  protected override void Invoke(object parameter)
89  {
91  if (args == null)
92  {
93  throw new ArgumentException(
94  "Expected Message with FileNameMessageContent, not " +
95  parameter.GetType().ToString());
96  }
97  _messageContent = args.Content;
98  string result = GetDialogResult(args.Content.Value, args.Content.Filter);
99  args.Content.Confirmed = !string.IsNullOrEmpty(result);
100  if (args.Content.Confirmed)
101  {
102  args.Content.Value = result;
103  };
104  args.Respond();
105  }
106 
107  #endregion
108 
109  #region Private fields
110 
111  private FileNameMessageContent _messageContent;
112 
113  #endregion
114  }
115 }
Provides access to the window handle of a WPF window or a Form or a custom handle.
Definition: Win32Window.cs:32
Abstract base class for the ChooseFileSaveAction and ChooseFolderAction classes.
Base class for actions that use dialogs based on System.Windows.Forms.FileDialog. ...
override string GetDialogResult(string defaultString, string filter)
Displays an dialog to choose file or folder names.