如何在Windows Phone ListBox.ItemTemplate全景应用程序中获取项目单击事件(选择了哪个项目)

时间:2014-02-08 18:12:54

标签: windows-phone-7 windows-phone-8 windows-phone

如何在Windows Phone ListBox.ItemTemplate中选择/触摸项目详细信息 以下是Build in template

的代码
<!--Panorama item one-->
    <controls:PanoramaItem Header="second item" Name="ptHeader1" >
            <!--Double line list with image placeholder and text wrapping-->
            <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="ListBox_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                            <!--Replace rectangle with image-->
                            <Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
                            <StackPanel Width="311">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PanoramaItem>

我尝试在.cs文件代码中使用代码。

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
        ptHeader1.Header = String.Format("   You selected {0}.", lbi.Content);
    }

但我无法获得所选的物品信息。

3 个答案:

答案 0 :(得分:0)

当您像使用ItemsSource="{Binding Items}"一样对ListBox进行数据绑定时,SelectionChanged处理程序中的SelectedItem属性包含其中一个项目,而不是ListBoxItem

尝试这样的事情:

YourClass selectedItem = (sender as ListBox).SelectedItem as YourClass;
if(selectedItem != null)
{
    var lineOne = selectedItem.LineOne;
}

答案 1 :(得分:0)

使用此代码选择ListBox项目:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBoxItemType lbi = ListBox.SelectedItem as ListBoxItemType;    
}

答案 2 :(得分:0)

我会这样做:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ptHeader1.Header = String.Format("   You selected {0}.", (sender as ListBox).SelectedText);
}