开发者

Can I binding a function defined in another user-control as current event handler?

I want to use开发者_运维技巧 a function defined in another user control as the event handler for this control. Is there any way I can fulfill it by using binding? Or the binding can only be used for attribute rather than event handler?

For example:

 <my:Search x:Name="queryControl" KeyDown=""/>
 <my:Measurement x:Name="measureTool" />

In the Measurement code behind:

 public partial class Measurement : UserControl {
    public void Measure2Tool(object sender, EventArgs e) {}
 }

Can I use binding to assign the Measure2Tool function as the event handler for the Search control's KeyDown event handler? How?

Thanks for your input!

Wei


Events do not accept binding, so you can't do that directly. What you can do to achieve that is use behaviors like the CallMethodAction with and EventTrigger (the event you want to handle).

<my:Search x:Name="queryControl>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
      <ei:CallMethodAction TargetObject="{Binding ElementName=measureTool}" 
                           MethodName="{Binding Measure2Tool}"/>
    </i:EventTrigger>
  <i:Interaction.Triggers>
<my:Search x:Name="queryControl>

with

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜