User Control Events Handling In Page Windows 8.1

User control events handling in page Windows 8.1

  1. Create a Windows Store blank app.

    app

  2. Add a user control in app,

    app

    app

  3. Add a button in user control within grid.
    1. <Grid>  
    2.     <Button Content="click me" Click="Button_Click" />  
    3. </Grid>  
  4. In code behind of user control create a event handler.
    1. public event RoutedEventHandler ButtonClicked;  
  5. Add user control to your main page,
    1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    2.     <local:MyUserControl1 HorizontalAlignment="Center" VerticalAlignment="Center" />  
    3. </Grid>  
  6. Rebuild you solution and add new event for button clicked event that was created in user control.
    1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    2.     <local:MyUserControl1 HorizontalAlignment="Center" VerticalAlignment="Center" ButtonClicked="MyUserControl1_ButtonClicked" />  
    3. </Grid>  
  7. Adding message dialog to verify button click event in main page.
    1. public MainPage()  
    2. {  
    3.     this.InitializeComponent();  
    4. }  
    5.   
    6. private async void MyUserControl1_ButtonClicked(object sender, RoutedEventArgs e)   
    7. {  
    8.     MessageDialog md = new MessageDialog("Button clicked");  
    9.     await md.ShowAsync();  
    10. }  
  8. Run application and check that a message is shown.

    application

Up Next
    Ebook Download
    View all
    Learn
    View all