WPF Custom Dictionary

adding custom dictionary in wpf 




MainWoindow.xaml file code

<Window x:Class="WpfApplication2.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:sys="clr-namespace:System;assembly=system"

        Title="Custom Dictionaries Example">

    <Grid Margin="0,0,0,-6">
        <RichTextBox Name="RichTextBox1" HorizontalAlignment="Left" SpellCheck.IsEnabled="True" Height="77" Margin="31,8,0,0" VerticalAlignment="Top" Width="704">
            <SpellCheck.CustomDictionaries>
                <sys:Uri>pack://application:,,,/dictionary.lex</sys:Uri>
            </SpellCheck.CustomDictionaries>
            <RichTextBox.ContextMenu>
                <ContextMenu></ContextMenu>
            </RichTextBox.ContextMenu>
        </RichTextBox>
    </Grid>

</Window>

MainWoindow.xaml.cs file


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            RichTextBox1.ContextMenuOpening += new ContextMenuEventHandler(rtfRitchTxtbox_ContextMenuOpening);
        }
        private void rtfRitchTxtbox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            var selection = RichTextBox1.Selection;
            if (rtb == null)
            {
                return;
            }

           
            ContextMenu menu = new ContextMenu();
            rtb.ContextMenu = menu;
            MenuItem item = new MenuItem();

            SpellingError spellingError = this.RichTextBox1.GetSpellingError(this.RichTextBox1.CaretPosition);
            if (spellingError != null)
            {
                //Creating the suggestions menu items.
                foreach (string suggestion in spellingError.Suggestions)
                {
                    MenuItem menuItem = new MenuItem();
                    menuItem.Header = suggestion;
                    menuItem.FontWeight = FontWeights.Bold;
                    menuItem.Command = EditingCommands.CorrectSpellingError;
                    menuItem.CommandParameter = suggestion;
                    menuItem.CommandTarget = this.RichTextBox1;
                    this.RichTextBox1.ContextMenu.Items.Add(menuItem);
                }
                if (this.RichTextBox1.ContextMenu.Items.Count == 0)
                {
                    //No Suggestions found, add a disabled NoSuggestions menuitem.
                    MenuItem menuItem = new MenuItem();
                    menuItem.Header = "No Suggestions";
                    menuItem.IsEnabled = false;
                    this.RichTextBox1.ContextMenu.Items.Add(menuItem);
                }
                Separator seperator = new Separator();
                this.RichTextBox1.ContextMenu.Items.Add(seperator);
                //Adding the IgnoreAll menu item
                MenuItem IgnoreAllMenuItem = new MenuItem();
                IgnoreAllMenuItem.Header = "Ignore All";
                IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
                IgnoreAllMenuItem.CommandTarget = this.RichTextBox1;
                this.RichTextBox1.ContextMenu.Items.Add(IgnoreAllMenuItem);
                Separator seperator1 = new Separator();
                this.RichTextBox1.ContextMenu.Items.Add(seperator1);

                MenuItem AddToDictionary = new MenuItem();
                AddToDictionary.Header = "Add to Dictionary";
                //Getting the word to add
                var tr = this.RichTextBox1.GetSpellingErrorRange(this.RichTextBox1.CaretPosition);
                //Ignoring the added word.
                AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
                AddToDictionary.CommandTarget = this.RichTextBox1;
                AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
                {
                    this.AddToDictionary(tr.Text);
                };
                this.RichTextBox1.ContextMenu.Items.Add(AddToDictionary);
            }

            item = new MenuItem();
            item.Command = ApplicationCommands.Cut;
            menu.Items.Add(item);
            item = new MenuItem();
            item.Command = ApplicationCommands.Copy;
            menu.Items.Add(item);
            item = new MenuItem();
            item.Command = ApplicationCommands.Paste;
            menu.Items.Add(item);
           
            menu.PlacementTarget = rtb;
            menu.Placement = PlacementMode.RelativePoint;
            TextPointer position = rtb.Selection.End;
            if (position == null)
            {
                return;
            }
            Rect positionRect = position.GetCharacterRect(LogicalDirection.Forward);
            menu.HorizontalOffset = positionRect.X;
            menu.VerticalOffset = positionRect.Y; menu.IsOpen = true;
            e.Handled = true;
        }
        private void AddToDictionary(string text)
        {
            TextWriter tw = new StreamWriter(@"c:\users\viva\documents\visual studio 2013\Projects\WpfApplication2\WpfApplication2\dictionary.lex", true);
            tw.WriteLine(text);
            tw.Close();
        }
        protected void AddToDictionary(object o, RoutedEventArgs rea)
        {

        }
    }
}

Comments

Popular posts from this blog

What is the importance of EDMX file in Entity Framework

TRIGGER in sql server

Sending Email in asp.net or mvc using gmail or other smpt.