Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Microsoft.Win32;
- namespace Project.Controls
- {
- public delegate void FilePathChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs args);
- /// <summary>
- /// Interaction logic for SelectFileBox.xaml
- /// </summary>
- public partial class SelectFileBox : UserControl
- {
- #region Dependency Properties
- /// <summary>
- /// DependencyProperty for the FilePath property
- /// </summary>
- public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
- "FilePath",
- typeof(string),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(null,
- FrameworkPropertyMetadataOptions.AffectsRender,
- new PropertyChangedCallback(OnFilePathChanged)
- )
- );
- /// <summary>
- /// DependencyProperty for the FilePathIsValid property
- /// </summary>
- public static readonly DependencyProperty FilePathIsValidProperty = DependencyProperty.Register(
- "FilePathIsValid",
- typeof(bool),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(false,
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the ButtonText property
- /// </summary>
- public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register(
- "ButtonText",
- typeof(string),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata("Select File...",
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the TextBoxBorderColor property
- /// </summary>
- public static readonly DependencyProperty TextBoxBorderColorProperty = DependencyProperty.Register(
- "TextBoxBorderColor",
- typeof(Brush),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(Brushes.Black,
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the TextBoxBorderThickness property
- /// </summary>
- public static readonly DependencyProperty TextBoxBorderThicknessProperty = DependencyProperty.Register(
- "TextBoxBorderThickness",
- typeof(Thickness),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(new Thickness{Bottom = 1.0, Left = 1.0, Right = 1.0, Top = 1.0},
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the TextBoxIsEnabled property
- /// </summary>
- public static readonly DependencyProperty TextBoxIsEnabledProperty = DependencyProperty.Register(
- "TextBoxIsEnabled",
- typeof(bool),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(false,
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the DefaultExt property
- /// </summary>
- public static readonly DependencyProperty DefaultExtProperty = DependencyProperty.Register(
- "DefaultExt",
- typeof(string),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata(".txt",
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- /// <summary>
- /// DependencyProperty for the Filter property
- /// </summary>
- public static readonly DependencyProperty FilterProperty = DependencyProperty.Register(
- "Filter",
- typeof(string),
- typeof(SelectFileBox),
- new FrameworkPropertyMetadata("Text documents (.txt)|*.txt",
- FrameworkPropertyMetadataOptions.AffectsRender
- )
- );
- #endregion Dependency Properties
- #region Properties
- /// <summary>
- /// The path to the file that was selected
- /// </summary>
- public string FilePath
- {
- get { return (string)GetValue(FilePathProperty); }
- set { SetValue(FilePathProperty, value); }
- }
- /// <summary>
- /// Wether or not the FilePath points to an existing file
- /// </summary>
- public bool FilePathIsValid
- {
- get { return (bool)GetValue(FilePathIsValidProperty); }
- set { SetValue(FilePathIsValidProperty, value); }
- }
- /// <summary>
- /// The Text in the Select Button
- /// </summary>
- public string ButtonText
- {
- get { return (string)GetValue(ButtonTextProperty); }
- set { SetValue(ButtonTextProperty, value); }
- }
- /// <summary>
- /// The color of the TextBox Border
- /// </summary>
- public Brush TextBoxBorderColor
- {
- get { return (Brush)GetValue(TextBoxBorderColorProperty); }
- set { SetValue(TextBoxBorderColorProperty, value); }
- }
- /// <summary>
- /// The thickness of the TextBox Border
- /// </summary>
- public Thickness TextBoxBorderThickness
- {
- get { return (Thickness)GetValue(TextBoxBorderThicknessProperty); }
- set { SetValue(TextBoxBorderThicknessProperty, value); }
- }
- /// <summary>
- /// Enable or disable the textbox
- /// </summary>
- public bool TextBoxIsEnabled
- {
- get { return (bool)GetValue(TextBoxIsEnabledProperty); }
- set { SetValue(TextBoxIsEnabledProperty, value); }
- }
- /// <summary>
- /// The path to the file that was selected
- /// </summary>
- public string DefaultExt
- {
- get { return (string)GetValue(DefaultExtProperty); }
- set { SetValue(DefaultExtProperty, value); }
- }
- /// <summary>
- /// The path to the file that was selected
- /// </summary>
- public string Filter
- {
- get { return (string)GetValue(FilterProperty); }
- set { SetValue(FilterProperty, value); }
- }
- #endregion Properties
- #region Constructor
- /// <summary>
- /// Default constructorr for the SelectFileBox
- /// </summary>
- public SelectFileBox()
- {
- InitializeComponent();
- FilePathChanged += TextBoxValidateInput;
- }
- #endregion
- #region Event triggers
- /// <summary>
- /// Function called when the FilePath has been changed, raises the FilePathChanged event
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- public static void OnFilePathChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
- {
- SelectFileBox s = ((SelectFileBox) sender);
- if (s.FilePathChanged != null)
- {
- s.FilePathChanged(sender, args);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private void TextBoxValidateInput(DependencyObject sender, DependencyPropertyChangedEventArgs args)
- {
- FilePathIsValid = File.Exists(FilePath);
- }
- /// <summary>
- /// Select a file and puts its path in the text box
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SelectFileClick(object sender, RoutedEventArgs e)
- {
- // Configure open file dialog box
- OpenFileDialog dlg = new OpenFileDialog();
- dlg.DefaultExt = DefaultExt; // Default file extension
- dlg.Filter = Filter; // Filter files by extension
- // Show open file dialog box
- Nullable<bool> result = dlg.ShowDialog();
- // Process open file dialog box results
- if (result == true)
- {
- // Open document
- FilePath = dlg.FileName;
- }
- }
- #endregion Events triggers
- #region Events
- /// <summary>
- /// Event that is called when the FilePath has been changed
- /// </summary>
- public event FilePathChangedHandler FilePathChanged;
- #endregion Events
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement