FileNameEditorControl.cs 5.7 KB

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