C# 控件初始化时未激发ControlTemplate EventTrigger
我有一个控件,它使用EventTriggers来启动/停止动画。除了第一次初始化控件时,EventTriggers没有被激发外,其他一切都正常工作。我怀疑当属性更改值时,它们尚未设置,因此不会触发和启动动画 如果在运行时更改C# 控件初始化时未激发ControlTemplate EventTrigger,c#,.net,wpf,xaml,controltemplate,C#,.net,Wpf,Xaml,Controltemplate,我有一个控件,它使用EventTriggers来启动/停止动画。除了第一次初始化控件时,EventTriggers没有被激发外,其他一切都正常工作。我怀疑当属性更改值时,它们尚未设置,因此不会触发和启动动画 如果在运行时更改IsBusy属性值,一切正常 我做错了什么 导致问题的用法(StartEvent已触发,但EventTrigger似乎无法捕获): 我控制的相关代码: /// <summary> /// Event that starts the anima
IsBusy
属性值,一切正常
我做错了什么
导致问题的用法(StartEvent已触发,但EventTrigger似乎无法捕获):
我控制的相关代码:
/// <summary>
/// Event that starts the animation
/// </summary>
public static readonly RoutedEvent StartEvent;
/// <summary>
/// Event that stops the animation
/// </summary>
public static readonly RoutedEvent StopEvent;
static BusyIndicator()
{
DefaultStyleKeyProperty.
OverrideMetadata(
typeof(BusyIndicator),
new FrameworkPropertyMetadata(typeof(BusyIndicator))
);
StartEvent =
EventManager.RegisterRoutedEvent(
"StartEvent",
RoutingStrategy.Direct,
typeof(RoutedEventHandler),
typeof(BusyIndicator));
StopEvent =
EventManager.RegisterRoutedEvent(
"StopEvent",
RoutingStrategy.Direct,
typeof(RoutedEventHandler),
typeof(BusyIndicator));
}
/// <summary>
/// Indicates if the control is currently animated.
///
/// This is a dependency property
/// </summary>
public bool IsBusy
{
get { return (bool)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}
/// <summary>
/// Identifier for the IsBusy property
/// </summary>
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register(
"IsBusy",
typeof(bool),
typeof(BusyIndicator),
new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsBusyChanged)));
private static void OnIsBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs inEventArgs)
{
bool newValue = (bool)inEventArgs.NewValue;
if (newValue)
{
System.Diagnostics.Debug.WriteLine("Start");
(sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StartEvent));
}
else
{
System.Diagnostics.Debug.WriteLine("Stop");
(sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StopEvent));
}
}
//
///启动动画的事件
///
公共静态只读RoutedEvent StartEvent;
///
///事件停止动画
///
公共静态只读RoutedEvent StopEvent;
静态总线指示器()
{
DefaultStyleKeyProperty。
复盖复盖数据(
类型(总线指示器),
新的FrameworkPropertyMetadata(类型of(BusyIndicator))
);
起始事件=
EventManager.RegisterRoutedEvent(
“StarteEvent”,
路线策略。直接,
类型(RoutedEventHandler),
(总线指示器)的类型;
停止事件=
EventManager.RegisterRoutedEvent(
“停止事件”,
路线策略。直接,
类型(RoutedEventHandler),
(总线指示器)的类型;
}
///
///指示控件当前是否已设置动画。
///
///这是一个依赖属性
///
公共图书馆很忙
{
获取{return(bool)GetValue(IsBusyProperty);}
set{SetValue(IsBusyProperty,value);}
}
///
///IsBusy属性的标识符
///
公共静态只读从属属性IsBusyProperty=
从属属性。寄存器(
“我很忙”,
类型(bool),
类型(总线指示器),
新UIPropertyMetadata(假,新PropertyChangedCallback(OnIsBusyChanged));
私有静态无效OnIsBusyChanged(DependencyObject发送方,DependencyPropertyChangedEventArgs inEventArgs)
{
bool newValue=(bool)inEventArgs.newValue;
如果(新值)
{
System.Diagnostics.Debug.WriteLine(“启动”);
(发送方作为总线指示器)。RaiseEvent(新路由EventTargets(StartEvent));
}
其他的
{
系统。诊断。调试。写入线(“停止”);
(发送方作为总线指示器)。RaiseEvent(新路由EventTargets(停止事件));
}
}
my Themes\generic.XAML文件中的相关XAML
如果模板尚未应用,则它无法侦听事件。您需要在应用程序模板后保留一个标志并引发事件,或者使用IsBusy属性的触发器并使用进入和退出 如果尚未应用模板,则无法侦听事件。您需要在应用程序模板后保留一个标志并引发事件,或者使用IsBusy属性的触发器并使用进入和退出 谢谢,我选择覆盖OnApplyTemplate,它工作得非常好。我曾尝试对IsBusy属性使用触发器,但由于出现“样式中的情节提要树无法指定TargetName”异常而失败。谢谢,我选择覆盖OnApplyTemplate,这非常有效。我曾尝试对IsBusy属性使用触发器,但由于“样式中的情节提要树无法指定TargetName”异常而失败。