简体   繁体   中英

How to handle Click event for Buttons in UserControl - Windows 8 app?

In Windows Store Apps, I create a UserControl to encapsulate and reuse code-behind and layout XAML.

"ForumItem.xaml" UserControl :

<UserControl>
    <Grid>
        <Button x:Name="YesButton" Content="Button" Grid.Row="1"/>
        <Button x:Name="NoButton" Content="Button" Grid.Column="1" Grid.Row="1" />
    </Grid>
</UserControl>

I Create ten items form my UserControl and then add it to Grid in Code-Behind for myPage :

Grid grid = new Grid()
for ( int i = 0 ; i<10 ; i++)
{
    ForumItem sss = new ForumItem();
    RowDefinition row = new RowDefinition();
    row.Height = new GridLength(1, GridUnitType.Star);
    grid.RowDefinitions.Add(row);
    Grid.SetRow(sss, flag);
    grid.Children.Add(sss);
}

I want to handle Click event for two Buttons in ten user control , I really appreciate any help. Thank you .

Declare NoClick and YesClick events in the UserControl. They respectively should execute the handlers for the Yes and No buttons. Expose them in the UserControl, and wire them up while you populate your grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM