FileNameEditorControl.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using InABox.Core;
  6. using Microsoft.Win32;
  7. namespace InABox.DynamicGrid
  8. {
  9. public class FileNameEditorControl : DynamicEditorControl<string, FileNameEditor>
  10. {
  11. static FileNameEditorControl()
  12. {
  13. //DynamicEditorControlFactory.Register<FileNameEditorControl, FileNameEditor>();
  14. }
  15. private TextBox Editor;
  16. public string Filter { get; set; }
  17. public bool AllowView { get; set; }
  18. public bool RequireExisting { get; set; }
  19. public override int DesiredHeight()
  20. {
  21. return 25;
  22. }
  23. public override int DesiredWidth()
  24. {
  25. return int.MaxValue;
  26. }
  27. public override void SetFocus()
  28. {
  29. Editor.Focus();
  30. }
  31. public override void SetColor(Color color)
  32. {
  33. Editor.Background = new SolidColorBrush(color);
  34. }
  35. public override void Configure()
  36. {
  37. Filter = EditorDefinition.FileMask;
  38. AllowView = EditorDefinition.AllowView;
  39. RequireExisting = EditorDefinition.RequireExisting;
  40. }
  41. protected override FrameworkElement CreateEditor()
  42. {
  43. var Grid = new Grid
  44. {
  45. VerticalAlignment = VerticalAlignment.Stretch,
  46. HorizontalAlignment = HorizontalAlignment.Stretch
  47. };
  48. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  49. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  50. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  51. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  52. Editor = new TextBox
  53. {
  54. VerticalAlignment = VerticalAlignment.Stretch,
  55. VerticalContentAlignment = VerticalAlignment.Center,
  56. HorizontalAlignment = HorizontalAlignment.Stretch,
  57. Margin = new Thickness(0, 0, 0, 0),
  58. IsEnabled = false
  59. };
  60. //Editor.LostFocus += (o, e) => CheckChanged();
  61. Editor.SetValue(Grid.ColumnProperty, 0);
  62. Editor.SetValue(Grid.RowProperty, 0);
  63. Grid.Children.Add(Editor);
  64. var Select = new Button
  65. {
  66. VerticalAlignment = VerticalAlignment.Stretch,
  67. VerticalContentAlignment = VerticalAlignment.Center,
  68. HorizontalAlignment = HorizontalAlignment.Stretch,
  69. Margin = new Thickness(5, 1, 0, 1),
  70. Width = 45,
  71. Content = "Select",
  72. Focusable = false
  73. };
  74. Select.SetValue(Grid.ColumnProperty, 1);
  75. Select.SetValue(Grid.RowProperty, 0);
  76. Select.Click += Select_Click;
  77. Grid.Children.Add(Select);
  78. var Clear = new Button
  79. {
  80. VerticalAlignment = VerticalAlignment.Stretch,
  81. VerticalContentAlignment = VerticalAlignment.Center,
  82. HorizontalAlignment = HorizontalAlignment.Stretch,
  83. Margin = new Thickness(5, 1, 0, 1),
  84. Width = 45,
  85. Content = "Clear",
  86. Focusable = false
  87. };
  88. Clear.SetValue(Grid.ColumnProperty, 2);
  89. Clear.SetValue(Grid.RowProperty, 0);
  90. Clear.Click += Clear_Click;
  91. Grid.Children.Add(Clear);
  92. if (AllowView)
  93. {
  94. var View = new Button
  95. {
  96. VerticalAlignment = VerticalAlignment.Stretch,
  97. VerticalContentAlignment = VerticalAlignment.Center,
  98. HorizontalAlignment = HorizontalAlignment.Stretch,
  99. Margin = new Thickness(5, 1, 0, 1),
  100. Width = 45,
  101. Content = "View",
  102. Focusable = false
  103. };
  104. View.SetValue(Grid.ColumnProperty, 3);
  105. View.SetValue(Grid.RowProperty, 0);
  106. View.Click += View_Click;
  107. Grid.Children.Add(View);
  108. }
  109. return Grid;
  110. }
  111. private void Select_Click(object sender, RoutedEventArgs e)
  112. {
  113. if (RequireExisting)
  114. {
  115. var dlg = new OpenFileDialog();
  116. dlg.Filter = Filter;
  117. dlg.Title = "Open File";
  118. if (dlg.ShowDialog() == true)
  119. {
  120. Editor.Text = dlg.FileName;
  121. CheckChanged();
  122. }
  123. ;
  124. }
  125. else
  126. {
  127. var sfd = new SaveFileDialog();
  128. sfd.Filter = Filter;
  129. sfd.Title = "Open File";
  130. sfd.AddExtension = true;
  131. sfd.OverwritePrompt = false;
  132. if (sfd.ShowDialog() == true)
  133. {
  134. Editor.Text = sfd.FileName;
  135. CheckChanged();
  136. }
  137. }
  138. }
  139. private void View_Click(object sender, RoutedEventArgs e)
  140. {
  141. if (string.IsNullOrWhiteSpace(Editor.Text))
  142. {
  143. MessageBox.Show("Please select a document first!");
  144. return;
  145. }
  146. var gsProcessInfo = new ProcessStartInfo();
  147. gsProcessInfo.Verb = "open";
  148. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  149. gsProcessInfo.FileName = Editor.Text;
  150. gsProcessInfo.UseShellExecute = true;
  151. Process.Start(gsProcessInfo);
  152. }
  153. private void Clear_Click(object sender, RoutedEventArgs e)
  154. {
  155. Editor.Text = "";
  156. CheckChanged();
  157. }
  158. protected override string RetrieveValue()
  159. {
  160. return Editor.Text;
  161. }
  162. protected override void UpdateValue(string value)
  163. {
  164. Editor.Text = value;
  165. }
  166. }
  167. }