c# – 如何以编程方式为控件创建事件触发器
发布时间:2020-12-15 04:12:05 所属栏目:百科 来源:网络整理
导读:我想以编程方式为ContentControl创建一个事件触发器.我希望获得与使用此xaml代码相同的结果.包括 – Command,CommandParameter,EventName 它在我的xaml代码中的外观如何: ContentControl i:Interaction.Triggers i:EventTrigger EventName="PreviewMouseLef
我想以编程方式为ContentControl创建一个事件触发器.我希望获得与使用此xaml代码相同的结果.包括 – Command,CommandParameter,EventName
它在我的xaml代码中的外观如何: <ContentControl> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseLeftButtonDown"> <i:InvokeCommandAction Command="{Binding ButtonClickCommand}" CommandParameter="btnAdd"/> </i:EventTrigger> </i:Interaction.Triggers> </ContentControl> 解决方法
这是代码中的等价物:
void SetTrigger(ContentControl contentControl) { // create the command action and bind the command to it var invokeCommandAction = new InvokeCommandAction { CommandParameter = "btnAdd" }; var binding = new Binding { Path = new PropertyPath("ButtonClickCommand") }; BindingOperations.SetBinding(invokeCommandAction,InvokeCommandAction.CommandProperty,binding); // create the event trigger and add the command action to it var eventTrigger = new System.Windows.Interactivity.EventTrigger { EventName = "PreviewMouseLeftButtonDown" }; eventTrigger.Actions.Add(invokeCommandAction); // attach the trigger to the control var triggers = Interaction.GetTriggers(contentControl); triggers.Add(eventTrigger); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |