123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Microsoft.Win32;
- namespace InABox.DynamicGrid
- {
- public class FileNameEditorControl : DynamicEditorControl<string>
- {
- private TextBox Editor;
- public string Filter { get; set; }
- public bool AllowView { get; set; }
- public bool RequireExisting { get; set; }
- public override int DesiredHeight()
- {
- return 25;
- }
- public override int DesiredWidth()
- {
- return int.MaxValue;
- }
- public override void SetFocus()
- {
- Editor.Focus();
- }
- public override void SetColor(Color color)
- {
- Editor.Background = new SolidColorBrush(color);
- }
- protected override FrameworkElement CreateEditor()
- {
- var Grid = new Grid
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- HorizontalAlignment = HorizontalAlignment.Stretch
- };
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- Editor = new TextBox
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- Margin = new Thickness(0, 0, 0, 0),
- IsEnabled = false
- };
- //Editor.LostFocus += (o, e) => CheckChanged();
- Editor.SetValue(Grid.ColumnProperty, 0);
- Editor.SetValue(Grid.RowProperty, 0);
- Grid.Children.Add(Editor);
- var Select = new Button
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- Margin = new Thickness(5, 1, 0, 1),
- Width = 45,
- Content = "Select",
- Focusable = false
- };
- Select.SetValue(Grid.ColumnProperty, 1);
- Select.SetValue(Grid.RowProperty, 0);
- Select.Click += Select_Click;
- Grid.Children.Add(Select);
- var Clear = new Button
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- Margin = new Thickness(5, 1, 0, 1),
- Width = 45,
- Content = "Clear",
- Focusable = false
- };
- Clear.SetValue(Grid.ColumnProperty, 2);
- Clear.SetValue(Grid.RowProperty, 0);
- Clear.Click += Clear_Click;
- Grid.Children.Add(Clear);
- if (AllowView)
- {
- var View = new Button
- {
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- Margin = new Thickness(5, 1, 0, 1),
- Width = 45,
- Content = "View",
- Focusable = false
- };
- View.SetValue(Grid.ColumnProperty, 3);
- View.SetValue(Grid.RowProperty, 0);
- View.Click += View_Click;
- Grid.Children.Add(View);
- }
- return Grid;
- }
- private void Select_Click(object sender, RoutedEventArgs e)
- {
- if (RequireExisting)
- {
- var dlg = new OpenFileDialog();
- dlg.Filter = Filter;
- dlg.Title = "Open File";
- if (dlg.ShowDialog() == true)
- {
- Editor.Text = dlg.FileName;
- CheckChanged();
- }
- ;
- }
- else
- {
- var sfd = new SaveFileDialog();
- sfd.Filter = Filter;
- sfd.Title = "Open File";
- sfd.AddExtension = true;
- sfd.OverwritePrompt = false;
- if (sfd.ShowDialog() == true)
- {
- Editor.Text = sfd.FileName;
- CheckChanged();
- }
- }
- }
- private void View_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(Editor.Text))
- {
- MessageBox.Show("Please select a document first!");
- return;
- }
- var gsProcessInfo = new ProcessStartInfo();
- gsProcessInfo.Verb = "open";
- gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
- gsProcessInfo.FileName = Editor.Text;
- gsProcessInfo.UseShellExecute = true;
- Process.Start(gsProcessInfo);
- }
- private void Clear_Click(object sender, RoutedEventArgs e)
- {
- Editor.Text = "";
- CheckChanged();
- }
- protected override string RetrieveValue()
- {
- return Editor.Text;
- }
- protected override void UpdateValue(string value)
- {
- Editor.Text = value;
- }
- }
- }
|