WPF 之Event
来源:互联网 发布:哪个地图数据最详细 编辑:程序博客网 时间:2024/06/06 02:06
WPF中的Event
Routed Events 意指一个事件在一个相互关联的元素组成的树中传递,而非直接传递到一个特定的目标。
它可以在两个方向进行传递,Bubbling, tunneling
事件Routed的三种方式
1〉 Bubbling:事件由源头传递到元素树的最顶点
2〉 tunneling:事件由树的顶端传递的源头
3〉 direct:事件不在树中移动,表现方式类同标准的CLR事件
如何写Routed Event
示例:
Public static readonly RoutedEvent TapEvent =
EventManager.RegisterRoutedEvent(“Tap”,
RoutingStrategy.Bubble,
typeof(RoutedEventHandle),
typeof(MyButtonSimple)
);
// Provide CLR accessors for the event
Public event RoutedEventHandle Tap
{
Add{AddHandler(TapEvent, value);}
Remove{RemoveHandler(TapEvent, value);}
}
如何在Xaml关联事件
<Button Click=”blSetColor”>button</Button>
为什么要使用Routed Event
对于一组需要交互的控件,可以将父元素作为一个通用的事件监听者,然后使用同一个事件函数来处理交互。
示例如下
A grouped button set
XAML
<Border Height="50" Width="300" BorderBrush="Gray" BorderThickness="1">
<StackPanel Background="LightGray" Orientation="Horizontal" Button.Click="CommonClickHandler">
<Button Name="YesButton" Width="Auto" >Yes</Button>
<Button Name="NoButton" Width="Auto" >No</Button>
<Button Name="CancelButton" Width="Auto" >Cancel</Button>
</StackPanel>
</Border>
C#
Private void CommonClickHandler(object sender, RoutedEventArgs e)
{
FrameworkElement feSource = e.Source as FrameworkElement;
Switch(feSource.Name)
{
Case “YesButton”:
// do something here
Break;
Case “NoButton”
// do something
Break;
Case “CancelButton”:
// do something
Break;
}
e.Handled = true;
}
自定义Event
示例如下:
//Invoke该事件
protected virtual void OnPipeValueChanged(double oldValue, double newValue)
{
RoutedPropertyChangedEventArgs<double> args = new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
args.RoutedEvent = PipeControl.PipeValueChangedEvent;
RaiseEvent(args);
}
// 注册该事件
public static readonly RoutedEvent PipeValueChangedEvent =
EventManager.RegisterRoutedEvent("PipeValueChanged",
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<double>),
typeof(PipeControl));
- WPF 之Event
- WPF Control event 触发
- WPF Event事件
- [.NET] Handle event for WPF
- WPF 路由事件 Event Routing
- Event事件对象之Event
- WPF Panel resize end event logic
- WPF--event、delegate、EventHandler、RoutedEventHandler的区别
- WPF的Event转化为Command
- wpf textbox mouse click event处理
- WPF初学者之WPF样式
- WPF开发之WPF简介
- WPF入门之hello WPF!
- mysql event----语法之CREATE EVENT
- C#学习之Event
- c#精华之event
- 多线程Event之详解
- mysql之event
- 使用resin而不是tomcat进行vxml开发
- 学习笔记:什么是设计模式?
- 网站title与meta的写法与作用
- 参数session_cached_cursors的详细解释
- 中兴和华为的面试经历
- WPF 之Event
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- ASP.NET 2.0中实现客户端回调的简化版
- 读写配置文件app.config
- oracle奇怪问题
- 实例学习Hibernate
- SetNextRule和SetRootRule的区别
- 张五常(著名经济学家)谈产品质量
- Provider (0x80004005)未指定的错误的解决方法