如何在Silverlight中通过依赖项属性实现点击事件? [英] How to achieve click event via dependency property in silverlight?

查看:59
本文介绍了如何在Silverlight中通过依赖项属性实现点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Silverlight的世界还很陌生,所以请多多包涵.我创建了一个自定义枢轴项目控件,以显示在枢轴控件中.现在,在此自定义控件中有一个按钮.现在,我可以将click事件处理程序添加到自定义控件的支持cs文件中的按钮上,这样就可以了.但是,有没有办法让我在自定义控件的声明期间指定自定义控件的按钮的事件处理程序?即在我的pivot_page.xaml中

I'm fairly new to the world of Silverlight so please bear with me. I have created a custom pivot item control to be displayed in a pivot control. Now in this custom control there's a button. Now i could just add the click event handler to the button in the custom control's backing cs file and that would be ok. But is there a way for me to specify the event handler of the custom control's button during the declaration of the custom control? i.e. something like this in my pivot_page.xaml

<custom:myPivotItem background="..." height=".." width=".." click="myHandler"/>

在pivot_page.cs中声明了myHandler的地方?谢谢

where myHandler is declared in pivot_page.cs? Thanks

推荐答案

通过在自定义控件cs文件(myCustomPivotItem.cs)中声明 RoutedEventHandler 解决了该问题.

Solved it by declaring a RoutedEventHandler in the custom controls cs file (myCustomPivotItem.cs).

 public event RoutedEventHandler Click;

然后在onApplyTemplate中,我可以使用

Then in onApplyTemplate I get access to the rectangle object with

 Rectangle rect = this.GetTemplateChild("rectObject") as Rectangle;
 rect.Tap += new EventHandler<GestureEventArgs>(RectView_Tap);

然后我在同一cs文件(myCustomPivotItem.cs)中声明了RectView_Tap

I then declared RectView_Tap in the same cs file (myCustomPivotItem.cs)

    private void RectView_Tap(object sender, GestureEventArgs e)
    {
        if (Click != null)
            Click(this, new RoutedEventArgs());
    }

在MainPage.xaml中,我像这样声明了自定义控件

in my MainPage.xaml i declared the custom control like so

 <controls:PivotItem x:Name="pivotitem2">
     <view:myCustomePivotItem x:Name="custompivotItem2" Click="myHandler"/>
 </controls:PivotItem>

在MainPage.cs中,我声明了myHandler ...

and in MainPage.cs i declared myHandler...

 void myHandler(object sender, RoutedEventArgs e)
    {
        //delegate operation
        MessageBox.Show("Clicked!");
    }

它按预期工作!!:)希望它能对需要的人有所帮助.

And it works as expected!! :) Hope it helps anyone who needs it.

这篇关于如何在Silverlight中通过依赖项属性实现点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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