Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Creating the Behavior

Listing 16.2 contains the entire code-behind file for Listing 16.1. This gives FileInputBox the appropriate behavior when the Button is clicked, exposes the text from the TextBox as a read/write property, and exposes a simple FileNameChanged event corresponding to the TextChanged event exposed by the TextBox. The event handler for TextChanged marks the event as handled (to stop its bubbling) and raises the FileNameChanged event instead.

Listing 16.2. FileInputBox.xaml.cs—The Logic for FileInputBox

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace Chapter16
{
  public partial class
							FileInputBox : UserControl
  {
    public FileInputBox()
    {
      InitializeComponent();
      theTextBox.TextChanged += new TextChangedEventHandler(OnTextChanged);
    }

    private void theButton_Click(object sender, RoutedEventArgs e)
    {
      OpenFileDialog d = new
							OpenFileDialog();
      if (d.ShowDialog() == true) // Result could be true, false, or null
							this.FileName = d.FileName;
    }

    public string FileName
    {
      get { return theTextBox.Text; }
set { theTextBox.Text = value; }
    }

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

    public event
							EventHandler<EventArgs> FileNameChanged;
  }
}


					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint