我有一個WPF MainWindow和我自己的RoutedEvent,當按下Enter鍵時會引發它。爲什麼在我的xaml中沒有處理自定義routedevent
namespace ZK16
{
public partial class MainWindow : Window
{
public static readonly RoutedEvent CustomEvent = EventManager.RegisterRoutedEvent(
"Custom", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler Custom
{
add { AddHandler(CustomEvent, value); }
remove { RemoveHandler(CustomEvent, value); }
}
void RaiseCustomEvent()
{
Debug.WriteLine("RaiseCustomEvent");
RoutedEventArgs newEventArgs = new RoutedEventArgs(MainWindow.CustomEvent);
RaiseEvent(newEventArgs);
}
public MainWindow()
{
InitializeComponent();
this.AddHandler(MainWindow.CustomEvent, new RoutedEventHandler(MyCustomHandler));
}
private void MyCustomHandler(object sender, RoutedEventArgs e)
{
Debug.WriteLine("In Eventhandler...");
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
RaiseCustomEvent();
}
在XAML我添加一個Label與一個EventTrigger,
<Window x:Class="ZK16.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ZK16"
Title="MainWindow"
PreviewKeyDown="Window_PreviewKeyDown"
>
<Grid>
<Label Content="Hello World">
<Label.Triggers>
<EventTrigger RoutedEvent="local:MainWindow.Custom">
<BeginStoryboard>
<Storyboard Duration="00:00:1">
<DoubleAnimation From="6" To="25" Storyboard.TargetProperty="FontSize"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Label.Triggers>
</Label>
</Grid>
</Window>
但提高自定義事件時, 的EventTrigger不會觸發我的事件,但如果觸發例如
EventTrigger RoutedEvent="Loaded"
有人可以告訴我,怎麼了?
很好的回答!非常感謝 –