Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows 8 Metro app, How to set a click event for different images that are loaded?
    primarykey
    data
    text
    <p>I've created a drag and drop function in my app which i've used a code sample and works ok. The images need to have a click event, so ive used the tapped event. However I need the event to have a different result.</p> <p>So basically, the xaml code for the image source is as so.</p> <pre><code>Image Source="{Binding Image}" Tapped="chordClicked" </code></pre> <p>and in the C#</p> <pre><code>protected override void OnNavigatedTo(NavigationEventArgs e) { source.Add(new Item("Assets/Achord.png")); source.Add(new Item("Assets/Bchord.png")); source.Add(new Item("Assets/Cchord.png")); source.Add(new Item("Assets/Dchord.png")); source.Add(new Item("Assets/Echord.png")); source.Add(new Item("Assets/Fchord.png")); source.Add(new Item("Assets/Gchord.png")); availableItems.ItemsSource = source; chosenItems.ItemsSource = destination; } </code></pre> <p>I tried a few variations on how I could go about doing this but none have worked, I thought the method below would make sense.</p> <p>for the time being the text block is just a way of testing if the click is working properly</p> <pre><code>private void chordClicked(object sender, TappedRoutedEventArgs e) { if (source = ("Assets/Achord.png")) { textBlock.Text = "A chord clicked"; } private void dragItem(object sender, DragItemsStartingEventArgs e) { draggedItem = (Item)e.Items[0]; } private void dropItemDestination(object sender, DragEventArgs e) { source.Remove(draggedItem); destination.Insert(0, draggedItem); } private void dropItemSource(object sender, DragEventArgs e) { destination.Remove(draggedItem); source.Add(draggedItem); } </code></pre> <p>Thank you in advance</p> <ul> <li>Bash.d</li> </ul> <p>Hi thanks for your help, I searched around other places and eventually found the solution on the msdn forums.</p> <pre><code>private void chordClicked(object sender, TappedRoutedEventArgs e) { var source = ((sender as Image).Source as BitmapImage).UriSource.LocalPath; if (source == "/Assets/Achord.png") { textBlock.Text = "A chord clicked"; } else if (source == "/Assets/Bchord.png") { textBlock.Text = "B chord clicked"; } else if (source == "/Assets/Cchord.png") { textBlock.Text = "C chord clicked"; } } </code></pre> <p>Which works fine.</p> <p>I appreciate your help a lot, I will learn more about using delegates and lambdas at some point in the near future</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload