| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 | namespace InABox.DynamicGrid;#if USEIMAGES    public class FileEditorControl<T> : DynamicEditorControl<DataFile> where T : DataFile, new()    {        private TextBox Editor = null;        T _value = null;        protected override FrameworkElement CreateEditor()        {            Grid 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(50, GridUnitType.Pixel) });            Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Pixel) });            Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50, GridUnitType.Pixel) });            Editor = new TextBox()            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,            };            Editor.TextChanged += (o, e) => Changed = true;            Editor.SetValue(Grid.ColumnProperty, 0);            Editor.SetValue(Grid.RowProperty, 0);            Grid.Children.Add(Editor);            Button Select = new Button()            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                Margin = new Thickness(5,0,0,0),                Content = "Select",                Focusable = false            };            Select.SetValue(Grid.ColumnProperty, 1);            Select.SetValue(Grid.RowProperty, 0);            Select.Click += Select_Click;            Grid.Children.Add(Select);            Button Clear = new Button()            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                Margin = new Thickness(5, 0, 0, 0),                Content = "Clear",                Focusable = false            };            Clear.SetValue(Grid.ColumnProperty, 2);            Clear.SetValue(Grid.RowProperty, 0);            Clear.Click += Clear_Click;            Grid.Children.Add(Clear);            Button View = new Button()            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                Margin = new Thickness(5, 0, 0, 0),                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 View_Click(object sender, RoutedEventArgs e)        {            if (String.IsNullOrEmpty(_value.FileName) || (_value.Data == null) || (_value.Data.Length == 0))            {                System.Windows.MessageBox.Show("No File Selected!");                return;            }            String ext = System.IO.Path.GetExtension(_value.FileName);            String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ext);            File.WriteAllBytes(filename, _value.Data);            ProcessStartInfo gsProcessInfo = new ProcessStartInfo();            gsProcessInfo.Verb = "open";            gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;            gsProcessInfo.FileName = filename;            gsProcessInfo.UseShellExecute = true;            Process.Start(gsProcessInfo);        }        private void Clear_Click(object sender, RoutedEventArgs e)        {            _value.FileName = "";            _value.Data = new byte[] { };            Editor.Text = "";        }        private void Select_Click(object sender, RoutedEventArgs e)        {            VistaOpenFileDialog dlg = new VistaOpenFileDialog();            dlg.Filter = _value.Filter();            if (dlg.ShowDialog() == true)            {                _value.FileName = System.IO.Path.GetFileName(dlg.FileName);                _value.Data = File.ReadAllBytes(dlg.FileName);                Editor.Text = _value.FileName;            }        }        protected override int DesiredHeight()        {            return 25;        }        protected override DataFile GetValue()        {            return _value;        }        protected override void SetValue(DataFile value)        {            T file = (T)value;            T newfile = new T();  //(DataFile)Activator.CreateInstance(file != null ? value.GetType() : typeof(MiscellaneousDataFile));            newfile.FileName = file != null ? file.FileName : "";            newfile.Data = file != null ? file.Data : new byte[] { };            Editor.Text = file != null ? file.FileName : "";            _value = newfile;        }        public override void SetFocus()        {            Editor.Focus();        }        public override void SetColor(Color color)        {            Editor.Background = new SolidColorBrush(color);        }    }    public class ImageFileEditorControl : FileEditorControl<ImageDataFile> { }    public class PDFFileEditorControl : FileEditorControl<PDFDataFile> { }    public class MiscellaneousFileEditorControl : FileEditorControl<MiscellaneousDataFile> { }#endif
 |