FileNameEditorControl.cs 6.1 KB

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