Windows Phone 8.1 网格视图 [英] Windows Phone 8.1 gridview
问题描述
如何在 gridview 中创建堆栈面板,堆栈面板中的各种元素响应不同的点击事件?
How can I create a stackpanel in a gridview with various elements in the stackpanel responding to different click event?
堆栈面板将包含 2 个 appbarbutton,每个 appbarbutton 都有一个文本块,该文本块根据点击次数增加...
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>
推荐答案
我已经使用 BehaviorsSDK 处理过同样的场景.此 SDK 通过添加为事件调用函数的功能使生活变得轻松.只需在您的项目中添加一个对 BehaviorsSDK 的引用,然后为每个事件为您的项目数据类添加一个方法.下面是一个例子:
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()
{
...
}
}
然后在您的 xamll 中:
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>
这篇关于Windows Phone 8.1 网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!