documentrich-text-editoruielement

Unable to set Button Background in RichTextBox


I have a RichTextBox to which I add a Button in code... I can set the Button's Foreground but not the Background.

                    Button b = new()
                    { // Unspecified width and button fills width
                        Content = "Test", 
                        FontSize = 20,
                        FontWeight = FontWeights.Bold,
                        Foreground = Brushes.Black,                        
                        Background = Brushes.Green,  
                        IsEnabled = true
                    };

                    // Add button to block, then insert before current block
                    BlockUIContainer blockUIContainer = new(b);
                    
                    Script.Document.Blocks.InsertBefore(block, blockUIContainer);

Solution

  • The catch! RichTextBox set

    IsDocumentEnabled="True"
    

    Once IsDocumentEnabled set true, added button responds normally (events now work). Even when RichTextBox has

    IsReadOnly="False"