getting information from a data binding listBox fo

2019-08-31 04:44发布

问题:

I have a news section that consist of listBox binding from a ViewModel (listBox include)

<controls:PanoramaItem x:Name="News" Header="News">
<!--Double line list with image placeholder and text wrapping-->
<ListBox x:Name="News_ListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}"SelectionChanged="SelectNewsItem">
<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 Name="NewsTitle" Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Name="NewsDetail" Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>

I want to do that when some one click on a news item it takes him to a new page and view the full information. I did the selectionChanged event but I don't know How to get the news information from the binding?

Plz help me. Thanks,

回答1:

a typical SelectionChanged handler for these cases should look like this:

private void lstItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (lstItems.SelectedIndex == -1) return;

    var item = lstItems.SelectedItem as MyClass;
    // do navigation here

    lstItems.SelectedIndex = -1;
}

This is a part of DataBound template in visual studio for WP7.