Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 - Image Click Event?

Is there a way to assign a click event to images? I would like to assign events to the delete and search buttons inside of my listbox that displays my data. Is there a way to do this using the image control or do I have to create a style in BLEND for a button?

        <ListBox x:Name="lbPills" ItemsSource="{Binding pillItemsCollection}" SelectionChanged="lbPills_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,0">*</TextBlock>
                        <StackPanel>
                            <TextBlock x:Name="ItemText" Text="{Binding Name}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        </StackPanel>

                        <Image Source="Images/delete.png" Margin="10,0"/>
                        <Image Source="Images/search.png" Margin="10,0"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
like image 571
webdad3 Avatar asked Jan 26 '11 04:01

webdad3


3 Answers

To my knowledge there are no listeners on the Image itself for click and gesture events (they will have to be attached via Gestures as previously mentioned). One way to approach this is to re-template the button:

        <Button>
            <Button.Template>
                <ControlTemplate>
                    <Image Source="Images/delete.png" Margin="10,0"/>
                </ControlTemplate>
            </Button.Template>
        </Button>

In setting the template on the button you will override the default template used by the phone (which has the extra padding, thick border, etc.). Using this method will allow you to tie into the button click event.

like image 95
avanek Avatar answered Nov 02 '22 03:11

avanek


You can use the gesture listener to detect tap (click) events. A walkthrough here.

WP7 Tip of the Day: Silverlight Toolkit: Gestures

Alternatively, you can place your image into a Button control and retemplate it in blend to have the appearance you want.

like image 26
Mick N Avatar answered Nov 02 '22 04:11

Mick N


Handle the ManipulationCompleted event (which is any tap, double-tap, swipe, caress or fondle) to your image(s). So:

<Image Source="Images/delete.png" Margin="10,0"/> becomes <Image x:Name="ImageDelete" ManipulationCompleted="ImageDelete_ManipulationCompleted" Source="Images/delete.png" Margin="10,0"/>. Then in the ImageDelete_ManipulationCompleted handler, track from whence it came in from the sender and do your thing.

If you want to only track a swipe instead of a tap, just do an if statement on the e.IsInertial from ManipulationCompletedEventArgs.

like image 27
Todd Main Avatar answered Nov 02 '22 02:11

Todd Main