我对Silverlight的世界相当新,所以请耐心等待。我创建了一个自定义枢轴项控件,以显示在枢轴控件中。现在在这个自定义控件中有一个按钮。现在我可以将click事件处理程序添加到自定义控件的后备cs文件中的按钮,这样就可以了。但有没有办法在自定义控件声明期间指定自定义控件按钮的事件处理程序?即我的pivot_page.xaml
中的类似内容<custom:myPivotItem background="..." height=".." width=".." click="myHandler"/>
在pivot_page.cs中声明myHandler?感谢
答案 0 :(得分:1)
您可以在自定义控件上公开一个映射到按钮单击事件的公共事件。
public event RoutedEventHandler Click
{
add { this.button.Click += value; }
remove { this.button.Click -= value; }
}
答案 1 :(得分:1)
通过在自定义控件cs文件(myCustomPivotItem.cs)中声明 RoutedEventHandler 来解决此问题。
public event RoutedEventHandler Click;
然后在onApplyTemplate中,我可以使用
访问矩形对象 Rectangle rect = this.GetTemplateChild("rectObject") as Rectangle;
rect.Tap += new EventHandler<GestureEventArgs>(RectView_Tap);
然后我在同一个cs文件中声明了RectView_Tap(myCustomPivotItem.cs)
private void RectView_Tap(object sender, GestureEventArgs e)
{
if (Click != null)
Click(this, new RoutedEventArgs());
}
在我的MainPage.xaml中,我声明了自定义控件,如此
<controls:PivotItem x:Name="pivotitem2">
<view:myCustomePivotItem x:Name="custompivotItem2" Click="myHandler"/>
</controls:PivotItem>
在MainPage.cs中我声明了myHandler ......
void myHandler(object sender, RoutedEventArgs e)
{
//delegate operation
MessageBox.Show("Clicked!");
}
它按预期工作!! :)希望它可以帮助任何需要它的人。