Unable to set Button Background in RichTextBox

16 Views Asked by At

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);
1

There are 1 best solutions below

0
TreeDragon On BEST ANSWER

The catch! RichTextBox set

IsDocumentEnabled="True"

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

IsReadOnly="False"