Click here to Skip to main content
15,886,067 members
Articles / Desktop Programming / WPF

A Reusable WPF Autocomplete TextBox (Part 2)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
24 Nov 2011CPOL6 min read 51.6K   3.2K   38  
This article is about a control that can be used to auto-omplete folders that have been typed into a textbox. It shows just how versatile binding and theming in WPF really is.
namespace Test
{
  using System;
  using System.Diagnostics;
  using System.IO;
  using System.Windows;
  using System.Xml.Serialization;

  using Aviad.WPF.Controls;

  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      this.InitializeComponent();

      ViewModel vm = this.Resources["vm"] as ViewModel;

      if (vm != null)
      {
        try
        {
          if (System.IO.File.Exists(this.SettingsFileName))
          {
            // Create a new file stream for reading the XML file
            using (FileStream readFileStream = new FileStream(this.SettingsFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
              // Create a new XmlSerializer instance with the type of the test class
              XmlSerializer serializerObj = new XmlSerializer(typeof(ViewModel));

              // Load the object saved above by using the Deserialize function
              ViewModel viewModel = (ViewModel)serializerObj.Deserialize(readFileStream);

              ((ViewModel)this.Resources["vm"]).SuggestEntries = viewModel.SuggestEntries;

              // Cleanup
              readFileStream.Close();
            }
          }
        }
        catch (Exception exp)
        {
          Debug.Print(exp.ToString());
        }
      }
    }

    private string SettingsFileName
    {
      get
      {
        return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                      "ACTBFolderSettings.xml");
      }
    }

    protected override void OnClosed(EventArgs e)
    {
      base.OnClosed(e);

      ViewModel vm = this.Resources["vm"] as ViewModel;

      if (vm != null)
      {
        if (vm.SuggestEntries.Count > 0)
        {
          try
          {
            // Create a new file stream to write the serialized object to a file
            using (TextWriter writeFileStream = new StreamWriter(this.SettingsFileName))
            {
              // Create a new XmlSerializer instance with the type of the test class
              XmlSerializer serializerObj = new XmlSerializer(typeof(ViewModel));

              serializerObj.Serialize(writeFileStream, vm);

              writeFileStream.Close(); // Cleanup
            }
          }
          catch (Exception exp)
          {
            Debug.Print(exp.ToString());
          }
        }
      }
    }

    /// <summary>
    /// A hyperlink has been clicked. Start a web browser and let it browse to where this points to...
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
    {
      Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
      e.Handled = true;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed Edi

and a few other projects on GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.

https://de.linkedin.com/in/dirkbahle

Comments and Discussions