WPF 获取绑定的事件处理程序绑定全局资源
2015-01-27 19:25
281 查看
1. 我们想要获取绑定到 button 上面的所有 click event handler actions. 可以使用以下的代码.
的handlers.
2. 如果有一个全局的资源, 例如一个List<string> 我们想要绑定到前台界面一个下拉框中. 做法如下:
首先把这个资源放到 Application Resource 中.
namespace RoutedEventHandlerInfoTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.btnTest.Click+=btnTest_Click; var infos = GetRoutedEventHandlers(this.btnTest, ButtonBase.ClickEvent); } private void btnTest_Click(object sender, RoutedEventArgs e) { Console.WriteLine(" button clicked"); } /// <summary> /// Gets the list of routed event handlers subscribed to the specified routed event. /// </summary> /// <param name="element">The UI element on which the event is defined.</param> /// <param name="routedEvent">The routed event for which to retrieve the event handlers.</param> /// <returns>The list of subscribed routed event handlers.</returns> public static RoutedEventHandlerInfo[] GetRoutedEventHandlers(UIElement element, RoutedEvent routedEvent) { // Get the EventHandlersStore instance which holds event handlers for the specified element. // The EventHandlersStore class is declared as internal. var eventHandlersStoreProperty = typeof(UIElement).GetProperty( "EventHandlersStore", BindingFlags.Instance | BindingFlags.NonPublic); object eventHandlersStore = eventHandlersStoreProperty.GetValue(element, null); // Invoke the GetRoutedEventHandlers method on the EventHandlersStore instance // for getting an array of the subscribed event handlers. var getRoutedEventHandlers = eventHandlersStore.GetType().GetMethod( "GetRoutedEventHandlers", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var routedEventHandlers = (RoutedEventHandlerInfo[])getRoutedEventHandlers.Invoke( eventHandlersStore, new object[] { routedEvent }); return routedEventHandlers; } } }分析下代码: 关键在 GetRoutedEventHandlers(UIElement, RoutedEvent) 方法. 这个方法首先从 UIElement 中获取一个名叫 EventHandlersStore 的私有实例属性. 通过 GetValue() 方法获取这个属性的值. 然后获取 EventHandlersStore 类的一个方法叫做 GetRoutedEventHandlers. 执行这个方法来获取 eventHandlersStore 实例中的类型为 routedEvent
的handlers.
2. 如果有一个全局的资源, 例如一个List<string> 我们想要绑定到前台界面一个下拉框中. 做法如下:
首先把这个资源放到 Application Resource 中.
System.Windows.Application.Current.Resources.Add("AllFlows", Launcher.AllFlows);然后, 在 xaml 文件中将这个资源用 DynamicResource 绑定到控件上.
<MultiSelectComboBox x:Name="cboIgnoreFlows" ItemsSource="{DynamicResource AllFlows}" SelectedValue="{Binding SelectedIgnoreFlows, Mode=TwoWay}">
相关文章推荐
- jquery之jquery事件处理模型(利用jquery绑定事件处理程序)
- 如何判断某个事件已经绑定了某个事件处理程序?
- off() 方法 与 unbind() 方法移除绑定事件的处理程序。one()函数用于为每个匹配元素的一个或多个事件绑定一次性事件处理函数
- javascript的事件流、绑定事件处理程序以及阻止事件流
- ASP.NET Google Maps Javascript API V3 实战基础篇一获取和设置事件处理程序中的属性
- EA&UML日拱一卒-0基础学习微信小程序(8)-事件处理和数据绑定
- 如何判断某个事件已经绑定了某个事件处理程序?
- IE attachEvent事件处理程序(事件绑定的函数)的this指向的是window不是执行当前事件的dom元素
- 们只是「电脑玩物」 首页 实用技巧 免费资源 超好玩的游戏 软件推荐 IT技术 资讯 编程 其它 MVVM设计模式和WPF中的实现(四) 事件绑定 07net01.com 发布于 2015-10-08
- ASP.NET Google Maps Javascript API V3 实战基础篇一获取和设置事件处理程序中的属性
- 利用反射绑定事件处理程序(C#)
- js使用函数绑定技术改变事件处理程序的作用域
- trigger 根据绑定到匹配元素的给定的事件类型执行所有的处理程序和行为。
- 通过给事件处理程序传递this参数,获取事件源对象的引用。单机提交按钮时在信息框中显示用户输入的字符。
- VS中使用VC资源编辑器添加变量或事件处理程序出现脚本错误的解决办法
- 事件处理程序的绑定
- 利用反射绑定事件处理程序(C#)
- 微信小程序 动态绑定数据及动态事件处理
- 利用反射绑定事件处理程序(C#)
- js中事件重复绑定会相应导致多次处理程序的响应