// Value (FixedChoice): The user has selected a new value
        // - set its corresponding value in the quickPasteItem data structure
        // - update the UI to show the new value
        private void FixedChoice_SelectionChanged(object sender, SelectionChangedEventArgs args)
        {
            ComboBox       comboBox       = sender as ComboBox;
            QuickPasteItem quickPasteItem = (QuickPasteItem)comboBox.Tag;

            quickPasteItem.Value = comboBox.SelectedValue.ToString();
        }
        // Value (Flags): The user has checked or unchecked a new value
        // - set its corresponding value in the quickPasteItem data structure
        // - update the UI to show the new value
        private void Flag_CheckedOrUnchecked(object sender, RoutedEventArgs e)
        {
            CheckBox       checkBox       = sender as CheckBox;
            QuickPasteItem quickPasteItem = (QuickPasteItem)checkBox.Tag;

            quickPasteItem.Value = checkBox.IsChecked.ToString();
        }
        // Value (Counters and Notes): The user has selected a new value
        // - set its corresponding value in the quickPasteItem data structure
        // - update the UI to show the new value
        private void NoteOrCounter_TextChanged(object sender, TextChangedEventArgs args)
        {
            TextBox        textBox        = sender as TextBox;
            QuickPasteItem quickPasteItem = (QuickPasteItem)textBox.Tag;

            quickPasteItem.Value = textBox.Text;
        }
        // Invoke when the user clicks the checkbox to enable or disable the data row
        private void UseCurrentRow_CheckChanged(object sender, RoutedEventArgs e)
        {
            CheckBox cbox = sender as CheckBox;

            // Enable or disable the controls on that row to reflect whether the checkbox is checked or unchecked
            int row = Grid.GetRow(cbox);

            TextBlock label = this.GetGridElement <TextBlock>(GridColumnLabel, row);
            UIElement value = this.GetGridElement <UIElement>(GridColumnValue, row);

            label.Foreground = cbox.IsChecked == true ? Brushes.Black : Brushes.Gray;
            value.IsEnabled  = cbox.IsChecked.Value;

            // Update the QuickPaste row data structure to reflect the current checkbox state
            QuickPasteItem quickPasteRow = (QuickPasteItem)cbox.Tag;

            quickPasteRow.Use    = cbox.IsChecked == true;
            this.Note.Visibility = this.QuickPasteEntry.IsAtLeastOneItemPastable() ? Visibility.Collapsed : Visibility.Visible;
        }
        // Given a quickPasteItem (essential the information representing a single data control and its value),
        // - add a row to the grid with controls that display that information,
        // - add a checkbox that can be selected to indicate whether that information should be included in a paste operation
        private void BuildRow(QuickPasteItem quickPasteItem, int gridRowIndex)
        {
            // USE Column: A checkbox to indicate whether the current search row should be used as part of the search
            Thickness thickness     = new Thickness(0, 2, 0, 2);
            CheckBox  useCurrentRow = new CheckBox()
            {
                Margin              = thickness,
                VerticalAlignment   = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                IsChecked           = quickPasteItem.Use,
                Tag = quickPasteItem
            };

            useCurrentRow.Checked   += this.UseCurrentRow_CheckChanged;
            useCurrentRow.Unchecked += this.UseCurrentRow_CheckChanged;
            this.UseCheckboxes.Add(useCurrentRow);

            Grid.SetRow(useCurrentRow, gridRowIndex);
            Grid.SetColumn(useCurrentRow, GridColumnUse);
            this.QuickPasteGridRows.Children.Add(useCurrentRow);

            // LABEL column: The label associated with the control (Note: not the data label)
            TextBlock controlLabel = new TextBlock()
            {
                Margin     = new Thickness(5),
                Text       = quickPasteItem.Label,
                Foreground = quickPasteItem.Use ? Brushes.Black : Brushes.Gray,
            };

            Grid.SetRow(controlLabel, gridRowIndex);
            Grid.SetColumn(controlLabel, GridColumnLabel);
            this.QuickPasteGridRows.Children.Add(controlLabel);

            // Value column: The value is presented as an editable field particular to its control type
            if (quickPasteItem.ControlType == Constant.Control.Note ||
                quickPasteItem.ControlType == Constant.Control.Counter)
            {
                // Notes and Counters both uses a text field, so they can be constructed as a textbox
                AutocompleteTextBox textBoxValue = new AutocompleteTextBox()
                {
                    Autocompletions          = null,
                    Text                     = quickPasteItem.Value,
                    Height                   = ValuesHeight,
                    Width                    = ValuesWidth,
                    TextWrapping             = TextWrapping.NoWrap,
                    VerticalAlignment        = VerticalAlignment.Center,
                    VerticalContentAlignment = VerticalAlignment.Center,
                    IsEnabled                = quickPasteItem.Use,
                    Tag = quickPasteItem
                };
                if (quickPasteItem.ControlType == Constant.Control.Note)
                {
                    textBoxValue.Autocompletions = this.dataEntryControls.AutocompletionGetForNote(quickPasteItem.DataLabel);
                }
                // Counter text fields are modified to only allow numeric input
                if (quickPasteItem.ControlType == Constant.Control.Counter)
                {
                    textBoxValue.PreviewTextInput += this.Counter_PreviewTextInput;
                    DataObject.AddPastingHandler(textBoxValue, this.Counter_Paste);
                }
                textBoxValue.TextChanged += this.NoteOrCounter_TextChanged;

                Grid.SetRow(textBoxValue, gridRowIndex);
                Grid.SetColumn(textBoxValue, GridColumnValue);
                this.QuickPasteGridRows.Children.Add(textBoxValue);
            }
            else if (quickPasteItem.ControlType == Constant.Control.FixedChoice)
            {
                // Choices use choiceboxes
                ControlRow controlRow    = this.fileDatabase.GetControlFromTemplateTable(quickPasteItem.DataLabel);
                ComboBox   comboBoxValue = new ComboBox()
                {
                    Height = ValuesHeight,
                    Width  = ValuesWidth,
                    // Create the dropdown menu
                    ItemsSource  = controlRow.GetChoicesForQuickPasteMenu(),
                    SelectedItem = quickPasteItem.Value,
                    IsEnabled    = quickPasteItem.Use,
                    Tag          = quickPasteItem
                };
                Grid.SetRow(comboBoxValue, gridRowIndex);
                Grid.SetColumn(comboBoxValue, GridColumnValue);
                this.QuickPasteGridRows.Children.Add(comboBoxValue);
                comboBoxValue.SelectionChanged += this.FixedChoice_SelectionChanged;
            }
            else if (quickPasteItem.ControlType == Constant.Control.Flag)
            {
                // Flags present checkable checkboxes
                CheckBox flagCheckBox = new CheckBox()
                {
                    VerticalAlignment   = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    IsChecked           = !string.Equals(quickPasteItem.Value, Constant.BooleanValue.False, StringComparison.OrdinalIgnoreCase),
                    IsEnabled           = quickPasteItem.Use,
                    Tag = quickPasteItem
                };
                flagCheckBox.Checked   += this.Flag_CheckedOrUnchecked;
                flagCheckBox.Unchecked += this.Flag_CheckedOrUnchecked;

                Grid.SetRow(flagCheckBox, gridRowIndex);
                Grid.SetColumn(flagCheckBox, GridColumnValue);
                this.QuickPasteGridRows.Children.Add(flagCheckBox);
            }
            else
            {
                // We should never get here
                throw new NotSupportedException(String.Format("Unhandled control type in QuickPasteEditor '{0}'.", quickPasteItem.ControlType));
            }
            this.Note.Visibility = this.QuickPasteEntry.IsAtLeastOneItemPastable() ? Visibility.Collapsed : Visibility.Visible;
        }