我想像这样建立一些非常直接的东西:
<EventTrigger RoutedEvent="MyCustomEvent">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="00:00:00.1" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
所以我可以在代码中调用类似的东西:
MyCustomEvent.trigger()
如何在WPF应用程序中以简单快捷的方式在C#中实现?
答案 0 :(得分:0)
您必须对s1
使用扩展方法,并使用RoutedEvent
方法。
示例:
UIElement.RaiseEvent
用法
XAML
public static class EventHandlerExtension
{
public static void TriggerThisEvent(this RoutedEvent routedEvt, FrameworkElement felem)
{
RoutedEventArgs args = new RoutedEventArgs(routedEvt, felem);
felem.RaiseEvent(args);
}
}
CODE
<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>