开发者

listbox scrolling and event handler problem, windows phone 7

开发者 https://www.devze.com 2023-03-19 01:48 出处:网络
So after solving one problem concerning listboxes, I face another. I have a listbox with multiple items in it. On these items, I have an event handler to know when a person selected the item. This is

So after solving one problem concerning listboxes, I face another.

I have a listbox with multiple items in it. On these items, I have an event handler to know when a person selected the item. This is a simple MouseLeftButtonDown event handler. However, whenever I try to scroll through the listbox, I cannot because the event fires when开发者_运维百科ever i lay my finger down.

In all, how do I go about making a scrollable listbox when I have event handlers detecting if a user selected a specific item within the listbox?

some code to show what I have on the page:

<Grid x:Name="LayoutRoot">
    <controls:Panorama x:Name="Community" Title="Community">

        <!--Panorama item one-->
        <controls:PanoramaItem x:Name="Groups" Header="Groups">
            <ListBox x:Name="People" Margin="0,0,-12,0" SelectionChanged="StackPanel_SelectionChanged">
               <ListBoxItem x:Name="Peter">
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                        <Image Height="100" Width="100" Source="/image002.jpg" Margin="12,0,9,0" Stretch="Fill" />
                        <StackPanel Width="311">
                            <TextBlock Text="Peter"  TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        </StackPanel>
                    </StackPanel>
            </ListBoxItem>
       .....
       //more of the same stackpanels as listed above, just with different Names

If you can imagine a row of these stackpanels, how would you be able to make it such that you can freely scroll through them?

MouseLeftButtonUp... It's not perfect. Please say that's not the only way. Is there an easy fix, such as placing a overlaying grid or scrollviewer, or something where I don't have to have manipulation started/completed events for this?

Help! :(


You can use the SelectionChanged event instead. Just note that it will only fire if the user selects an item that isn't currently selected. This might not be a problem if you're simply navigating to another page after the selected, but you could always use listbox.SelectedItem = -1 (after the event has fired) to reset the selection value if need be.

Edit: Code request from comment: So set your event to fire this handler (in the same way you set up the LeftMouseUp event):

private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   //your handler code (I've used code from your previous question)
   var namers="";

   var lbi = sender as ListBoxItem;
   if(lbi != null)
   {
       namers= lbi.Name.ToString();


       //Although optional, it might be worth resetting the selection value 
       //as long as you don't need the value anymore
       myListBox.SelectedItem = -1;

       //Navigate to the next page
       NavigationService.Navigate(new Uri("/Page2.xaml?name=" + namers, UriKind.Relative));
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消