I want to build something very straighforward like this:
<EventTrigger RoutedEvent="MyCustomEvent">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:00.1" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
So that I can call something like this in the code:
MyCustomEvent.trigger()
How can I do it, simple and fast, in C#, in a WPF application?
You have to use an extension method for a RoutedEvent
, and use UIElement.RaiseEvent
method.
Sample :
public static class EventHandlerExtension
{
public static void TriggerThisEvent(this RoutedEvent routedEvt, FrameworkElement felem)
{
RoutedEventArgs args = new RoutedEventArgs(routedEvt, felem);
felem.RaiseEvent(args);
}
}
Usage
XAML
<TextBox x:Name="Tbx" Text="type something here...">
<TextBox.Triggers>
<EventTrigger RoutedEvent="LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:03" Storyboard.TargetProperty="Opacity" To="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
CODE
private void Btn1_Click(object sender, RoutedEventArgs e)
{
TextBox.LostFocusEvent.TriggerThisEvent(Tbx);
}
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.