1
votes

How can I create a stackpanel in a gridview with various elements in the stackpanel responding to different click event?

The stackpanel will contain 2 appbarbutton and each appbarbutton has a textblock that increments based on the number of clicks...

        <GridView ItemsSource={Binding}>
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <AppBarButton Icon="Like"
                          Name="like"
                          IsCompact="True"
                          Click="like_Click"/>
                            <TextBlock Name="numOfLike"
                       Text="{Binding No_Positive_Likes}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <AppBarButton Icon="Dislike"
                          Name="dislike"
                          IsCompact="True"
                          Click="dislike_Click"/>
                            <TextBlock Name="numOfDisLike"
                       Text="{Binding No_negative_Likes}"/>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Vertical"
                    Tapped="loadQuestionAnswer_Click">
                        <TextBlock Name="question"
                       Text="What is my name"
                       FontSize="30"/>
                        <TextBlock Text="2013-12-10"/>
                    </StackPanel>

                </StackPanel>

            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
1
Please add some sample codeMitan Shah
Generally with DataTemplate, but it is unclear what exactly you want to achieve. AppBarButtons should belong on to the AppBar..Filip
@MitanShah I have updated the question....Gade Ifeoluwa
@Filip I have updated the question....Gade Ifeoluwa
And what is the problem? You can get tapped Grid item in your event, using DataContext on the sender..Filip

1 Answers

2
votes

I have handled this same scenario using the BehaviorsSDK.This SDK makes life easy by adding the ability to call a function for an event. Just add a reference in your project to the BehaviorsSDK and then add a method to your item data class for each event. Here is an example:

ObservableCollection<ItemModel> Items;

public class ItemModel
{
    public bool No_Positive_Likes ... 
    public bool No_Negative_Likes ...

    public void LikeButtonPressed()
    {
        ...
    }
    public void DislikeButtonPressed()
    {
        ...
    }
}

Then in your xamll:

    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    ...

    <GridView ItemsSource={Binding}>
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal">
                        <AppBarButton Icon="Like"
                            Name="like"
                            IsCompact="True">
                            <i:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Click">
                                    <core:CallMethodAction TargetObject="{Binding Mode=OneWay}" MethodName="LikeButtonPressed"/>
                                </core:EventTriggerBehavior>
                            </i:Interaction.Behaviors>
                        </AppBarButton>