Advertisement
Guest User

Untitled

a guest
Oct 27th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.00 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using Microsoft.Win32;
  7.  
  8. namespace Project.Controls
  9. {
  10.     public delegate void FilePathChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs args);
  11.  
  12.     /// <summary>
  13.     /// Interaction logic for SelectFileBox.xaml
  14.     /// </summary>
  15.     public partial class SelectFileBox : UserControl
  16.     {
  17.         #region Dependency Properties
  18.         /// <summary>
  19.         /// DependencyProperty for the FilePath property
  20.         /// </summary>
  21.         public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
  22.                 "FilePath",
  23.                 typeof(string),
  24.                 typeof(SelectFileBox),
  25.                 new FrameworkPropertyMetadata(null,
  26.                     FrameworkPropertyMetadataOptions.AffectsRender,
  27.                     new PropertyChangedCallback(OnFilePathChanged)
  28.                 )
  29.             );
  30.  
  31.         /// <summary>
  32.         /// DependencyProperty for the FilePathIsValid property
  33.         /// </summary>
  34.         public static readonly DependencyProperty FilePathIsValidProperty = DependencyProperty.Register(
  35.                 "FilePathIsValid",
  36.                 typeof(bool),
  37.                 typeof(SelectFileBox),
  38.                 new FrameworkPropertyMetadata(false,
  39.                     FrameworkPropertyMetadataOptions.AffectsRender
  40.                 )
  41.             );
  42.  
  43.         /// <summary>
  44.         /// DependencyProperty for the ButtonText property
  45.         /// </summary>
  46.         public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register(
  47.                 "ButtonText",
  48.                 typeof(string),
  49.                 typeof(SelectFileBox),
  50.                 new FrameworkPropertyMetadata("Select File...",
  51.                     FrameworkPropertyMetadataOptions.AffectsRender
  52.                 )
  53.             );
  54.  
  55.         /// <summary>
  56.         /// DependencyProperty for the TextBoxBorderColor property
  57.         /// </summary>
  58.         public static readonly DependencyProperty TextBoxBorderColorProperty = DependencyProperty.Register(
  59.                 "TextBoxBorderColor",
  60.                 typeof(Brush),
  61.                 typeof(SelectFileBox),
  62.                 new FrameworkPropertyMetadata(Brushes.Black,
  63.                     FrameworkPropertyMetadataOptions.AffectsRender
  64.                 )
  65.             );
  66.  
  67.         /// <summary>
  68.         /// DependencyProperty for the TextBoxBorderThickness property
  69.         /// </summary>
  70.         public static readonly DependencyProperty TextBoxBorderThicknessProperty = DependencyProperty.Register(
  71.                 "TextBoxBorderThickness",
  72.                 typeof(Thickness),
  73.                 typeof(SelectFileBox),
  74.                 new FrameworkPropertyMetadata(new Thickness{Bottom = 1.0, Left = 1.0, Right = 1.0, Top = 1.0},
  75.                     FrameworkPropertyMetadataOptions.AffectsRender
  76.                 )
  77.             );
  78.  
  79.         /// <summary>
  80.         /// DependencyProperty for the TextBoxIsEnabled property
  81.         /// </summary>
  82.         public static readonly DependencyProperty TextBoxIsEnabledProperty = DependencyProperty.Register(
  83.                 "TextBoxIsEnabled",
  84.                 typeof(bool),
  85.                 typeof(SelectFileBox),
  86.                 new FrameworkPropertyMetadata(false,
  87.                     FrameworkPropertyMetadataOptions.AffectsRender
  88.                 )
  89.             );
  90.  
  91.         /// <summary>
  92.         /// DependencyProperty for the DefaultExt property
  93.         /// </summary>
  94.         public static readonly DependencyProperty DefaultExtProperty = DependencyProperty.Register(
  95.                 "DefaultExt",
  96.                 typeof(string),
  97.                 typeof(SelectFileBox),
  98.                 new FrameworkPropertyMetadata(".txt",
  99.                     FrameworkPropertyMetadataOptions.AffectsRender
  100.                 )
  101.             );
  102.        
  103.         /// <summary>
  104.         /// DependencyProperty for the Filter property
  105.         /// </summary>
  106.         public static readonly DependencyProperty FilterProperty = DependencyProperty.Register(
  107.                 "Filter",
  108.                 typeof(string),
  109.                 typeof(SelectFileBox),
  110.                 new FrameworkPropertyMetadata("Text documents (.txt)|*.txt",
  111.                     FrameworkPropertyMetadataOptions.AffectsRender
  112.                 )
  113.             );
  114.         #endregion Dependency Properties
  115.  
  116.  
  117.         #region Properties
  118.         /// <summary>
  119.         /// The path to the file that was selected
  120.         /// </summary>
  121.         public string FilePath
  122.         {
  123.             get { return (string)GetValue(FilePathProperty); }
  124.             set { SetValue(FilePathProperty, value); }
  125.         }
  126.  
  127.         /// <summary>
  128.         /// Wether or not the FilePath points to an existing file
  129.         /// </summary>
  130.         public bool FilePathIsValid
  131.         {
  132.             get { return (bool)GetValue(FilePathIsValidProperty); }
  133.             set { SetValue(FilePathIsValidProperty, value); }
  134.         }
  135.  
  136.         /// <summary>
  137.         /// The Text in the Select Button
  138.         /// </summary>
  139.         public string ButtonText
  140.         {
  141.             get { return (string)GetValue(ButtonTextProperty); }
  142.             set { SetValue(ButtonTextProperty, value); }
  143.         }
  144.  
  145.         /// <summary>
  146.         /// The color of the TextBox Border
  147.         /// </summary>
  148.         public Brush TextBoxBorderColor
  149.         {
  150.             get { return (Brush)GetValue(TextBoxBorderColorProperty); }
  151.             set { SetValue(TextBoxBorderColorProperty, value); }
  152.         }
  153.  
  154.         /// <summary>
  155.         /// The thickness of the TextBox Border
  156.         /// </summary>
  157.         public Thickness TextBoxBorderThickness
  158.         {
  159.             get { return (Thickness)GetValue(TextBoxBorderThicknessProperty); }
  160.             set { SetValue(TextBoxBorderThicknessProperty, value); }
  161.         }
  162.  
  163.         /// <summary>
  164.         /// Enable or disable the textbox
  165.         /// </summary>
  166.         public bool TextBoxIsEnabled
  167.         {
  168.             get { return (bool)GetValue(TextBoxIsEnabledProperty); }
  169.             set { SetValue(TextBoxIsEnabledProperty, value); }
  170.         }
  171.  
  172.         /// <summary>
  173.         /// The path to the file that was selected
  174.         /// </summary>
  175.         public string DefaultExt
  176.         {
  177.             get { return (string)GetValue(DefaultExtProperty); }
  178.             set { SetValue(DefaultExtProperty, value); }
  179.         }
  180.  
  181.         /// <summary>
  182.         /// The path to the file that was selected
  183.         /// </summary>
  184.         public string Filter
  185.         {
  186.             get { return (string)GetValue(FilterProperty); }
  187.             set { SetValue(FilterProperty, value); }
  188.         }
  189.         #endregion Properties
  190.  
  191.  
  192.         #region Constructor
  193.         /// <summary>
  194.         /// Default constructorr for the SelectFileBox
  195.         /// </summary>
  196.         public SelectFileBox()
  197.         {
  198.             InitializeComponent();
  199.             FilePathChanged += TextBoxValidateInput;
  200.         }
  201.         #endregion
  202.  
  203.  
  204.         #region Event triggers
  205.         /// <summary>
  206.         /// Function called when the FilePath has been changed, raises the FilePathChanged event
  207.         /// </summary>
  208.         /// <param name="sender"></param>
  209.         /// <param name="args"></param>
  210.         public static void OnFilePathChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  211.         {
  212.             SelectFileBox s = ((SelectFileBox) sender);
  213.             if (s.FilePathChanged != null)
  214.             {
  215.                 s.FilePathChanged(sender, args);
  216.             }
  217.         }
  218.  
  219.         /// <summary>
  220.         ///
  221.         /// </summary>
  222.         /// <param name="sender"></param>
  223.         /// <param name="args"></param>
  224.         private void TextBoxValidateInput(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  225.         {
  226.             FilePathIsValid = File.Exists(FilePath);
  227.         }
  228.  
  229.         /// <summary>
  230.         /// Select a file and puts its path in the text box
  231.         /// </summary>
  232.         /// <param name="sender"></param>
  233.         /// <param name="e"></param>
  234.         private void SelectFileClick(object sender, RoutedEventArgs e)
  235.         {
  236.             // Configure open file dialog box
  237.             OpenFileDialog dlg = new OpenFileDialog();
  238.             dlg.DefaultExt = DefaultExt; // Default file extension
  239.             dlg.Filter = Filter; // Filter files by extension
  240.  
  241.             // Show open file dialog box
  242.             Nullable<bool> result = dlg.ShowDialog();
  243.  
  244.             // Process open file dialog box results
  245.             if (result == true)
  246.             {
  247.                 // Open document
  248.                 FilePath = dlg.FileName;
  249.             }
  250.  
  251.         }
  252.         #endregion Events triggers
  253.  
  254.  
  255.         #region Events
  256.         /// <summary>
  257.         /// Event that is called when the FilePath has been changed
  258.         /// </summary>
  259.         public event FilePathChangedHandler FilePathChanged;
  260.         #endregion Events
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement