Simple spelling correction user interface

The sample project SpellingCorrectionUIProvider Simple shows how to set the suggestions in the System.Windows.Controls.ListBox automatically and the IsEnabled state for each System.Windows.Controls.Button without using additional code. Furthermore, it explains how to connect a TextControl to the SpellingCorrectionUIProvider and handle its misspelled words by using the Change, Delete and Ignore methods.

The source code is contained in the following directories:

Relevant API links:

There are 2 different places in the project that need to be handled:

1. The XAML code, where the IsEnabled bindings are set for Buttons and the suggestion ListBox. Additionally, the ListBox's content is updated by an ItemsSource binding with the SuggestionsHandler.Suggestions property and each Button gets a Click event to force the corresponding method.

<ListBox Name="listBoxSuggestions" Grid.Column="1" Margin="7"
	IsEnabled="{Binding Source={StaticResource txSpellChecker1},Path=SpellingCorrectionUIProvider.SuggestionsHandling.IsSuggestionListEnabled}"
	ItemsSource ="{Binding Source={StaticResource txSpellChecker1}, Path=SpellingCorrectionUIProvider.SuggestionsHandling.Suggestions}">
</ListBox>


<Button Name="buttonChange" VerticalAlignment="Top" Margin="7" Content="Change"
	Click="buttonChange_Click"
	IsEnabled="{Binding Source={StaticResource txSpellChecker1}, Path=SpellingCorrectionUIProvider.CorrectionHandling.IsChangeEnabled}">
</Button>


<Button Name="buttonIgnore" VerticalAlignment="Top" Grid.Column="1" Margin="7" Content="Ignore"
	Click="buttonIgnore_Click"
	IsEnabled="{Binding Source={StaticResource txSpellChecker1}, Path=SpellingCorrectionUIProvider.CorrectionHandling.IsIgnoreEnabled}">
</Button>


<Button Name="buttonDelete" VerticalAlignment="Top" Grid.Column="2" Margin="7" Content="Delete"
	Click="buttonDelete_Click"
	IsEnabled="{Binding Source={StaticResource txSpellChecker1}, Path=SpellingCorrectionUIProvider.CorrectionHandling.IsDeleteEnabled}">
</Button>

[C#]
private void buttonChange_Click(object sender, RoutedEventArgs e)
{
	// change the current misspelled word with the suggestion selected in the ListBox
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.SelectedItem.ToString());
}

private void buttonIgnore_Click(object sender, RoutedEventArgs e)
{
	// ignore the current misspelled word
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Ignore();
}

private void buttonDelete_Click(object sender, RoutedEventArgs e)
{
	// delete the current misspelled word
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Delete();
}
[Visual Basic]
Private Sub buttonChange_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	' change the current misspelled word with the suggestion selected in the ListBox
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.SelectedItem.ToString)
End Sub

Private Sub buttonIgnore_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	' ignore the current misspelled word
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Ignore()
End Sub

Private Sub buttonDelete_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	' delete the current misspelled word
	txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Delete()
End Sub

2. The TextControl_Loaded event, where the SpellingCorrectionUIProvider class is connected to the TextControl. Furthermore a StateManaging_PropertyChanged event is defined to detect whether a misspelled word is currently available or not.

[C#]
private void textControl1_Loaded(object sender, RoutedEventArgs e)
{
	...
	// attach an event to the property changed event
	txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(StateManaging_PropertyChanged);
	// connect the TextControl to the TX Spell correction user interface manager
	txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.SetCurrent(textControl1);
	...
}

private void StateManaging_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
	// select the misspelled word in the TextControl
	if (e.PropertyName == "CurrentWordToCorrect")
	{
		if (txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect != null)
		{
			((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Select();
		}
		else {
			// if no misspelled word is available select nothing
			textControl1.Select(0, 0);
		}
	}
}
[Visual Basic]
Private Sub textControl1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
	...
	' attach an event to the property changed event
	AddHandler txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.PropertyChanged, AddressOf StateManaging_PropertyChanged
	' connect the TextControl to the TX Spell correction user interface manager
	txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.SetCurrent(textControl1)
	...
End Sub

Private Sub StateManaging_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
	' select the misspelled word in the TextControl
	If e.PropertyName = "CurrentWordToCorrect" Then
		If txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect IsNot Nothing Then
			DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect, TXTextControl.MisspelledWord).[Select]()
		Else
			' if no misspelled word is available select nothing
			textControl1.[Select](0, 0)
		End If
	End If
End Sub