C# 向富文本框组件添加事件
我试图将事件添加到我每次在富文本框中创建的标签中。下面是代码。 在我的类MainWindowController中,我有:C# 向富文本框组件添加事件,c#,.net,wpf,wpf-controls,C#,.net,Wpf,Wpf Controls,我试图将事件添加到我每次在富文本框中创建的标签中。下面是代码。 在我的类MainWindowController中,我有: public static Window MainWindow = new Windows.MainWindow(); public static Pages.MainPage MainPage = new Pages.MainPage(); public static Pages.LoginPage LoginPage = new Pages.LoginPage(); p
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();
在另一个类中,我有这个方法,每次客户端通过网络收到更新状态消息时,我都调用它
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");
}
一切正常,唯一的问题是富文本框中的UI元素不会“监听”事件,从此事件就不会触发。消息框从不显示。我尝试将Dispatcher.Invoke命令添加到下面的方法中,但它拒绝工作,并且事件从未触发。我希望你们都明白我想做什么
TLDR:StatusLabel\u鼠标指针从不开火
您需要在RichTextBox上将
IsDocumentEnabled
属性设置为true
获取或设置一个值,该值指示用户是否可以与RichTextBox中的UIElement和ContentElement对象交互
richtextbox文档中的事件有点棘手
对于某些事件,您可能还必须将控件放入BlockUIContainer中
即使这样,有些事情也不会像你所期望的那样起作用
单击已经有了意义,因此您可能会发现它已经在处理单击事件。使用preview events-PreviewMouseDown和PreviewMouseUp进行此类操作。感谢您的回复!我确实启用了该设置,现在事件应该触发了!