WPF Browse Usercontrol


Visual studio WPF

Over the last few weeks I have been working with WPF,  I have worked with Visual studios forms projects for more then 10 years.    This was my first chance to really sit down and get into Windows presentation foundation and learn what it was all about.      It hasn’t been easy there have been some learning issues.    MVVM for starters, xaml isn’t the same as windows forms.

 Browse directory user control

One of the most interesting things I have found with WPF is that there aren’t a lot of out of the box controls.   It seams that we have to make our own,  and you know what?   I like that I can make it look exactly how I want.

WPF Browse directory UserControl

This is my first usercontrol,  I am putting it here because I suspect i will need it again someday. It is a simple directory browse button, returns the text into the text box.

 BrowserControl.xaml

 


    
        
            
        
        
            
            
        
        
        

 

 BrowserControl.xaml

 

using System;
using System.Windows;
using System.Windows.Controls;


namespace WPFUserControl
{
    public partial class BrowserControl : UserControl
    {
        public BrowserControl()
        {
            InitializeComponent();
        }

        public string FileName
        {

            get { return BrowseTextBox.Text; }

            set { BrowseTextBox.Text = value; }

        }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
                this.FileName = dialog.SelectedPath;          

        }
        public event EventHandler FileNameChanged;

        private void BrowseTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            e.Handled = true;
            if (FileNameChanged != null)
                FileNameChanged(this, EventArgs.Empty);
        }      
    }
}

 

Usage


    
        
            
            
        
        
        

Conclusion

Learning anything takes time. I think we can agree that WPF is no exception in this, but don’t give up the results are very nice. I am still new at this so if anyone notices any major errors in what I am doing please feel free to let me know.
 


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.