0

I am stuck in a situation where i want to call OnTap event of my user control inside a listbox. The issue is that the event is fired every time I tap the user control, but I want to call all of the tap events at once (based on listbox items). For this I have created a user control in which I have overridden the OnTap event:

protected override void OnTap(System.Windows.Input.GestureEventArgs e)
{
  //Get Reciter and download in Reciter folder
  if (!downloadClicked)
  {
    downloadClicked=true;
    if (!string.IsNullOrEmpty(SuraNumber))
    {
      verseNumber = 0;
      verseByverseFiles = new List<string>();
      selectedReciter = DataSource.getSelectedReciter();
      DataSource.addVerseByVerseFiles(Convert.ToByte(SuraNumber), verseByverseFiles, selectedReciter);
      txtProgress.Text = "Downloading...";
      downloadFile();
    }
  }
  base.OnTap(e);
}

When I tap an item in the listbox, this event is called, which is okay.

I created a button which I want to call individual listbox item tap events, at the click of button.

Below is my failed attempt (My user control is inside listbox as an item):

<ListBox
  x:Name="lsbQuranData"
  Grid.Row="2" Foreground="Black"
  ScrollViewer.VerticalScrollBarVisibility="Visible"
>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Width="480">
        <local:SuraWithProgressBar
          Width="480"
          SuraNumber ="{Binding SuraNumber, Mode=TwoWay}"
          SuraName="{Binding SuraTName}"
          Tap="DownloadSura"
        ></local:SuraWithProgressBar>
        <Line
          X1="0"
          X2="480"
          Y1="0"
          Y2="0"
          VerticalAlignment="Bottom"
          StrokeThickness="2"
          Stroke="Black"
        />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

The button click event as below:

private void Button_Click(object sender, RoutedEventArgs e)
{
  if (!downloadClicked)
  {
    downloadClicked = true;
    for (int rowIndex = 0; rowIndex < 114; rowIndex++)
    {
      //Download All (Call individual tap event of the user control
    }
  }
  else
    MessageBox.Show("Please wait, downloading...");
}

private void DownloadSura(object sender, System.Windows.Input.GestureEventArgs e)
{

}

How do I call individual user controls' tap events?

Thanks!

Magus
  • 1,302
  • 9
  • 16
ARH
  • 1,566
  • 3
  • 25
  • 56
  • So, you want top call "Download" on all items of the ListBox? You should put a "Download" method in the Model (or viewmodel) itself. So if the ItemsSource of your ListBox is ICollection, then MyItem should have a Download method. Then just foreach the collection and call the Download method. – Shawn Kendrot Jan 23 '14 at 17:33
  • This helped me to sort out my problem.[Call OnTap event of user control inside lisbox all at once][1] [1]: http://stackoverflow.com/questions/13678211/handling-event-of-user-control-in-its-holding-pages-code-behind – ARH Jan 26 '14 at 08:47
  • This helped me to sort out my problem. http://stackoverflow.com/questions/13678211/handling-event-of-user-control-in-its-holding-pages-code-behind – ARH Jan 26 '14 at 08:48

1 Answers1

0

Straight forward I would encapsulate the code from the ListBoxItem's eventhandler into a method and call this method from the ListBoxItem's eventhandler or from the BUtton's eventhandler. Or did I get something wrong?

edit:

Try to get the ListBox via it's x:Name and iterate the ItemsCollection. After that you have the ListBoxItems and could try to perform your actions. I don't know what happens in DataSource.getSelectedReciter(); or DataSource.addVerseByVerseFiles(Convert.ToByte(SuraNumber), verseByverseFiles, selectedReciter); But you should work around the programmatically event raising. I don't think that works.

csteinmueller
  • 2,427
  • 1
  • 21
  • 32
  • This is what I did, When I call method It goes to the method (DownloadSura which is tap event of items inside listbox), but it doesn't call the override one, but When I TAP the control with hand it call the Override method. (that is why I am confused, When tapping its calls the override but when calling the method it doesn't) – ARH Jan 23 '14 at 16:45
  • That wouldn't work, because you are just calling the eventhandler. But the control's event is not raised. So neither the overrridden `OnTap` event not the default `OnTap` event is called. – csteinmueller Jan 23 '14 at 17:03
  • Thanks, This is the question, how can I do it, please? – ARH Jan 23 '14 at 17:06
  • I can get individual user control, but I don't know how to invoke the method? In my user control, I don't have Click event to override it and that way I can create my custom Click event handler. (Does User Control supports Click?) – ARH Jan 23 '14 at 17:12
  • Do you have to? What makes the call of OnTap individual for each control? Separate it into a function and call it from whereever you want. – csteinmueller Jan 23 '14 at 17:14
  • My user control has progress bar, when clicked it shows progress downloading each file, Which means each item in list box shows downloading progress. I can't achieve this using simply function. – ARH Jan 23 '14 at 17:21
  • Thanks alot, this helped me to sort out my problem (http://stackoverflow.com/questions/13678211/handling-event-of-user-control-in-its-holding-pages-code-behind) – ARH Jan 23 '14 at 18:21