在代码后面附加行为 [英] Attach behavior in code behind

查看:69
本文介绍了在代码后面附加行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户控件中使用了以下Xaml,该控件用作属性网格内的编辑器.问题是,c#从后面的代码中附加行为会是什么样子?

I have the following Xaml that is used in a user control that used as an editor inside a property grid. The question is, what would the c# look like to attach a behavior from the code behind?

<i:Interaction.Behaviors>
    <igExt:XamComboEditorSelectedItemsBehavior SelectedItems="{Binding SelectedItems, ElementName=_uc}"/>
</i:Interaction.Behaviors>

由于这是在PropertyGrid中动态加载的编辑器上,所以我只想创建一个编辑器实例,并从后面的代码进行绑定,而不必拥有实际上很短且仅包含一个编辑器的不同xaml文件

Since this is on an editor that is loaded dynamically in a PropertyGrid, I was just going to create an instance of the editor with binding from code behind rather than having to have different xaml files that are really short and just contain one editor.

还是简单地重新实现行为中的所有代码并在后面的代码中创建编辑器时调用它会更容易?

Or would it be easier to simply re implement all of the code that is in the Behavior and call it while I'm creating the editor in the code behind?

推荐答案

XamComboEditorSelectedItemsBehavior behavior = new XamComboEditorSelectedItemsBehavior();
behavior.SetBinding(XamComboEditorSelectedItemsBehavior.SelectedItemsProperty, new Binding() 
    { 
        ElementName = "_uc", 
        Path = new PropertyPath("SelectedItems"), 
        Mode = BindingMode.TwoWay 
    });
Interaction.GetBehaviors(yourElementName).Add(behavior)

这篇关于在代码后面附加行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆