سؤال

وأود أن إضافة عنصر قائمة إلى ContextMenu الافتراضية لRichTextBox.

وأنا يمكن إنشاء قائمة سياق جديدة ولكن بعد ذلك تفقد اقتراحات التدقيق الإملائي التي تظهر في القائمة الافتراضية.

هل هناك طريقة لإضافة عنصر دون-تنفيذ إعادة كل شيء؟

هل كانت مفيدة؟

المحلول

وانها ليست صعبة للغاية لreimplement قائمة السياق RichTextBox مع اقتراحات الهجاء وقص، لصق، وما إلى ذلك.

وعقف حفل الافتتاح قائمة السياق كما يلي:

AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);

وضمن معالج الأحداث بناء قائمة السياق ما تحتاج إليه. يمكنك إعادة عناصر القائمة السياق القائمة الحالية بما يلي:

private IList<MenuItem> GetSpellingSuggestions()
{
    List<MenuItem> spellingSuggestions = new List();
    SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
    if (spellingError != null)
    {
        foreach (string str in spellingError.Suggestions)
        {
            MenuItem mi = new MenuItem();
            mi.Header = str;
            mi.FontWeight = FontWeights.Bold;
            mi.Command = EditingCommands.CorrectSpellingError;
            mi.CommandParameter = str;
            mi.CommandTarget = myRichTextBox;
            spellingSuggestions.Add(mi);
        }
    }
    return spellingSuggestions;
}

private IList<MenuItem> GetStandardCommands()
{
    List<MenuItem> standardCommands = new List();

    MenuItem item = new MenuItem();
    item.Command = ApplicationCommands.Cut;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Copy;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Paste;
    standardCommands.Add(item);

    return standardCommands;
}

إذا كان هناك أخطاء الهجاء ويمكنك إنشاء تجاهل الكل مع:

MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "Ignore All";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
ignoreAllMI.CommandTarget = textBox;
newContextMenu.Items.Add(ignoreAllMI);

إضافة فواصل كما هو مطلوب. إضافة تلك العناصر إلى قائمة السياق الجديدة، ثم قم بإضافة MenuItems جديدة لامعة الخاص بك.

وانا ذاهب الى مواصلة البحث عن وسيلة للحصول على قائمة السياق الفعلية الرغم من ذلك، لأن هذا هو ذات الصلة لشيء سوف يكون العمل عليها في المستقبل القريب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top