In C # events, only a class declaring them can be called. In the case of a button, there is a method called OnClick that calls ClickEvent, but it is protected. So you need to declare a class that inherits from Button and change the visibility of the OnClick method (or declare some method that calls base.OnClick)
public class MyButton : Button
{
public new void OnClick()
{
base.OnClick();
}
}
XAML example
<StackPanel Background="White" >
<my:MyButton x:Name="TestButton" Click="HandleClick" Content="Test" />
<TextBlock x:Name="Result" />
</StackPanel>
And the code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
new Timer(TimerCall,null,0,1000);
}
private void TimerCall(object state)
{
Dispatcher.BeginInvoke(()=>TestButton.OnClick());
}
private void HandleClick(object sender, RoutedEventArgs e)
{
Result.Text = String.Format("Clicked on {0:HH:mm:ss}",DateTime.Now);
}
}
Although it is always easier to call an event handler.
HandleClick(this,null)
Then there is no need for additional plumbing.
Bulat source
share