我想以编程方式为ContentControl创建一个事件触发器.我希望获得与使用此xaml代码相同的结果.包括 – Command,CommandParameter,EventName
它在我的xaml代码中的外观如何:
它在我的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); }
原文地址:https://www.jb51.cc/csharp/92539.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。