简体   繁体   中英

How to programmatically click a TextBlock?

I have a TextBlock MouseDown event:

tb.MouseDown += (s, e) => {
  ToSp.Children.Add(CreateTbAlp((s as TextBlock).Text, ToSp));
  CheckAnswer();
};

And in Unit Test I need to test it. I tried:

TextBlockAutomationPeer peer = new TextBlockAutomationPeer(tb);
IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProv.Invoke();

But in this case, I get the error

System.NullReferenceException

same with this one

typeof (TextBlock)
  .GetMethod("MouseDown", BindingFlags.Instance | BindingFlags.NonPublic)
  .Invoke(tb, new object[0]);

Is there any other way to click TextBlock programmatically?

TextBlock.RaiseEvent(new RoutedEventArgs(TextBlock.MouseDown));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM