akrant_iOSDeveloper
akrant_iOSDeveloper

Reputation: 373

How to create custom control with buttons and how to add event button click event in silverlight for windows mobile 7

as i am following link for creating custom control for my application. http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps

can anyone tell me how to add button in custom control and add event handler for click event in that?

I added click event using following piece of code `

public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Button btn1, btn2, btn3, btn4;

            btn1 = GetTemplateChild("MyButton1") as Button;
            btn1.Click += new RoutedEventHandler(btn1_Click);
            btn2 = GetTemplateChild("MyButton2") as Button;
            btn2.Click += new RoutedEventHandler(btn2_Click);
            btn3 = GetTemplateChild("MyButton3") as Button;
            btn3.Click += new RoutedEventHandler(btn3_Click);
            btn4 = GetTemplateChild("MyButton4") as Button;
            btn4.Click += new RoutedEventHandler(btn4_Click);
        }`

Now if i want to navigate to some page after clicking buttons.

how to do that?? I am not getting "NavigationService.Navigate" option in button click event.

thanx in advance. :)

Upvotes: 3

Views: 3241

Answers (1)

Jobi Joy
Jobi Joy

Reputation: 50028

Once you added the Button in to the XAML, access the Button instance inside the OnApplyTemplate() method and then subscribe the Click event handler.

Button btn = this.GetTemplateChild("myButton") as Button;
btn.Click += new RoutedEventHandler(_btn_Click);

Upvotes: 3

Related Questions