Reputation: 29
I'm trying to add events to a label that I create every time in a rich textbox. Here is the code below. In my class MainWindowController I have:
public static Window MainWindow = new Windows.MainWindow();
public static Pages.MainPage MainPage = new Pages.MainPage();
public static Pages.LoginPage LoginPage = new Pages.LoginPage();
public static FlowDocument ChatDocument = new FlowDocument();
public static Paragraph ChatParagraph = new Paragraph();
And in another class I have this method and I call it each time the client receives an update status message through the network.
private static void HandleStatus(string MessageString)
{
string Sender = AuxiliaryClientWorker.GetElement(MessageString, "-U ", " -Content");
string Message = AuxiliaryClientWorker.GetElement(MessageString, "-Content ", ".");
MainWindowController.MainWindow.Dispatcher.Invoke(() =>
{
#region Status updater
System.Windows.Controls.Label StatusLabel = new System.Windows.Controls.Label();
StatusLabel.FontSize = 18;
StatusLabel.FontStyle = FontStyles.Oblique;
StatusLabel.FontStyle = FontStyles.Italic;
StatusLabel.Foreground = System.Windows.Media.Brushes.Black;
StatusLabel.Content = Sender + " has updated their status to " + Message;
StatusLabel.MouseEnter += StatusLabel_MouseEnter;
#endregion
MainWindowController.ChatParagraph.Inlines.Add(StatusLabel);
MainWindowController.ChatParagraph.Inlines.Add(Environment.NewLine);
MainWindowController.ChatDocument.Blocks.Add(MainWindowController.ChatParagraph);
MainWindowController.MainPage.ClientChatTextBox.Document = MainWindowController.ChatDocument;
});
}
private static void StatusLabel_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
MessageBox.Show("This");
}
Everything works fine, the only problem is that the UI elements within the rich textbox do not "listen" for events, henceforth the events never fire. The MessageBox never shows. I tried adding the Dispatcher.Invoke command to the method below and it just refuses to work and the event never fires. I hope you all understand what I'm trying to do!
TLDR: StatusLabel_MouseEnter never fires
Upvotes: 0
Views: 425
Reputation: 12276
You need to set the IsDocumentEnabled
property to true on your RichTextBox.
Gets or sets a value that indicates whether the user can interact with UIElement and ContentElement objects within the RichTextBox.
Events from a richtextbox document are kind of tricky.
You might also have to put a control in a BlockUIContainer for some events.
Even then, some things won't quite work as you might expect.
Clicking already has meaning so you will likely find it already handling click events. Use preview events - PreviewMouseDown and PreviewMouseUp for that sort of thing.
Upvotes: 1