using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Win32; namespace CustomWPF { public partial class FileBrowserBox : System.Windows.Controls.UserControl { public FileBrowserBox() { InitializeComponent(); } #region FilePath Property public String FilePath { get { return pathText.Text; } set { pathText.Text= value; } } #endregion // FilePath Property #region OutlineBrush Property /// /// The Dependency Property for the /// OutlineBrush property. /// public static readonly DependencyProperty OutlineBrushProperty = DependencyProperty.Register( "OutlineBrush", typeof(Brush), typeof(FileBrowserBox), new FrameworkPropertyMetadata(Brushes.Black, new PropertyChangedCallback(OnOutlineBrushChanged))); /// /// The Outline Brush used for the /// FileBrowserBox control. /// public Brush OutlineBrush { get { return (Brush)GetValue(OutlineBrushProperty); } set { SetValue(OutlineBrushProperty, value); } } /// /// Static Event that is called when the property is changed. /// /// The object that has changed. /// The arguments to the property. private static void OnOutlineBrushChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { FileBrowserBox control = (FileBrowserBox)obj; control.UpdateOutlineBrush(); } #endregion // OutlineBrush Property void UpdateOutlineBrush() { pathText.BorderBrush = OutlineBrush; browseButton.BorderBrush = OutlineBrush; } public void browseButton_Click(object sender, RoutedEventArgs args) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; if (dlg.ShowDialog().GetValueOrDefault()) { pathText.Text = dlg.FileName; } } public void controlBorder_SizeChanged(object sender, SizeChangedEventArgs args) { if (args.WidthChanged) { double newWidth = args.NewSize.Width - browseButton.ActualWidth; if (newWidth > browseButton.ActualWidth) { pathText.Width = newWidth; } else { pathText.Width = 0; } } } } }