bhaku
bhaku

Reputation: 448

navigating to a new page on clicking an item in the listbox

I am building an app where i need to navigate to another page on clicking an item in the listbox. I have tried a lot but was not able to do it. When clicked nothing happens. Please see my xaml and cs file and check if there is any problem in my coding.

My xaml is:

<ListBox Name="listBox1" SelectionChanged="listBox1_SelectionChanged"  Height="676" VerticalAlignment="Bottom">
    <ListBox.ItemTemplate>
        <DataTemplate>
              <Button>
                 <Button.Content>
                   <ScrollViewer HorizontalScrollBarVisibility="Auto" Height="80" Width="400">
                       <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
                           <Image Source="{Binding ImageBind }" Height="80" Width="120"/>
                           <StackPanel Orientation="Vertical">
                               <TextBlock Text="{Binding Path=News_Title}" TextWrapping="Wrap"></TextBlock>
                               <!-- <TextBlock Text="{Binding Path=News_Description}" TextWrapping="Wrap"></TextBlock>-->
                               <TextBlock Text="{Binding Path=Date_Start}" TextWrapping="Wrap" ></TextBlock>
                           </StackPanel>
                       </StackPanel>
                   </ScrollViewer>
               </Button.Content>
           </Button>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

My cs file is:

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // If selected index is -1 (no selection) do nothing

    if (listBox1.SelectedIndex == -1)
        return;

    // Navigate to the new page
    NavigationService.Navigate(new Uri("/NewsDetails.xaml?selectedItem=" + listBox1.SelectedIndex, UriKind.Relative));

    // Reset selected index to -1 (no selection)
    listBox1.SelectedIndex = -1;
}

Upvotes: 0

Views: 677

Answers (1)

Chris Shao
Chris Shao

Reputation: 8231

I think you can add property of IsHitTestVisible="False" to Button. So the listBox1_SelectionChanged method can be excuted.

Upvotes: 1

Related Questions