C# 是否删除xaml样式的事件处理程序?
是否有方法删除在另一个样式中定义的样式中的事件处理程序 下面是一个人为的例子:C# 是否删除xaml样式的事件处理程序?,c#,wpf,xaml,event-handling,C#,Wpf,Xaml,Event Handling,是否有方法删除在另一个样式中定义的样式中的事件处理程序 下面是一个人为的例子: <Style TargetType="{x:Type TextBox}" x:Key="DefaultTextBoxStyle"> <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/> <EventSetter Event="LostFocus" Handler="TextBox_LostFocus"/>
<Style TargetType="{x:Type TextBox}" x:Key="DefaultTextBoxStyle">
<EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
<EventSetter Event="LostFocus" Handler="TextBox_LostFocus"/>
<EventSetter Event="PreviewKeyUp" Handler="TextBox_PreviewKeyUp"/>
</Style>
<Style TargetType="{x:Type TextBox}" x:Key="InlineTextBox" BasedOn="{DynamicResource DefaultTextBoxStyle}">
<EventSetter Event="GotFocus" Handler="????"/> // set to nothing
<EventSetter Event="LostFocus" Handler="????"/> // set to nothing
<EventSetter Event="PreviewKeyUp" Handler="????"/> // set to nothing
</Style>
//一事无成
//一事无成
//一事无成
谢谢 从阅读开始,您必须有一个虚拟事件来设置e.Handled。EventSetter声明,“指定为BasedOn的样式中的事件设置程序处理程序将在立即样式上的处理程序之后调用。”因此,这将阻止BasedOn中的任何事件设置程序运行,除非它将自身标记为HandleEventsTo
<Style TargetType="{x:Type TextBox}"
x:Key="EatEvents"
BasedOn="{StaticResource OtherStyle}">
<EventSetter Event="Click" Handler="EatEventsHandler"/>
</Style>
public void EatEventsHandler(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
public void EatEventsHandler(对象发送方,routedventargs e)
{
e、 已处理=正确;
}
不起作用。。。这就像做this.GotFocus=null
,这是不合法的。您只能通过+=和-=读取…来访问事件,这是最基本的:DThanks!似乎是目前最好的解决方案。