如何在列表中的stackpanel上进行onclick?我有一个从WCF服务检索的数据列表。我目前的代码如下:
<ListBox x:Name="Results" Height="450" HorizontalAlignment="Left" VerticalAlignment="Bottom" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20,0,0,20" >
<TextBlock Text="{Binding SearchVal}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
<TextBlock Text="{Binding Category}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我去了很多网站研究,我累了鼠标不能,Tap,Up了很多..
我已经使用了你提供的代码,但在ItemTapped中,我没有按钮,所以我改为按钮到TextBlock。但它不会起作用。
//I try changing button to TextBlock, wont work.
var selectedSearchBtn = sender as Button; //Assuming your trigger is a button UI element.
if (selectedSearchBtn != null)
{
var selectedVal = selectedSearchBtn.DataContext as TestMap.Classes.Global.Place;
Classes.Global.searchedValue = "Test to get into the loop in main";
if (selectedVal != null)
{
Classes.Global.posy = selectedVal.Y;
Classes.Global.posx = selectedVal.X;
}
NavigationService.GoBack();
}
答案 0 :(得分:1)
<ListBox x:Name="Results" Height="450" HorizontalAlignment="Left" VerticalAlignment="Bottom" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20,0,0,20" Tap="ItemTapped">
<TextBlock Text="{Binding SearchVal}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
<TextBlock Text="{Binding Category}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void ItemTapped(object sender, GestureEventArgs e)
{
var element = sender as FrameworkElement; //
if (element != null)
{
var selectedVal = element.DataContext as TestMap.Classes.Global.Place;
Classes.Global.searchedValue = "Test to get into the loop in main";
if (selectedVal != null)
{
Classes.Global.posy = selectedVal.Y;
Classes.Global.posx = selectedVal.X;
}
NavigationService.GoBack();
}
}