Good day,
I'm getting desperate here. Consider the following use case.
I have a listbox which item's are custom templated control. It has several buttons for which this custom control has event handlers in code behind. Whenever button is clicked I call a method of the object my custom control is bound to through the DataContext. So, when user clicks stop, I call _context.stopDownload() and object does the rest.
But, I have one button which should start the playback of the content. I am trying to somehow listen to this button's click event on the core level, not in the code behind of my custom control which represents listbox's item UI.
So, to summarize:
- I have a core object which loads list of items into observable collection.
- This core object then gets the listbox object using GetTemplateChildnren. When this is done, core sets the ItemsSource of the listbox control to observable collection I get on #1 step. The listbox gets rendered with custom template control as its item's (DataTemplate used).
- I need to link event handler on the core object's level to the button element of custom control inside the DataTemplate.
I can't figure out #3. Among other things, I've tried doing something like that after I set the ItemsSource to wire the event handler, but container is always null.
DownloadsListElement.ItemsSource = _downloadsList;
foreach (var item in DownloadsListElement.ItemsSource)
{
var container = DownloadsListElement.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
}
I have also tried button a grid over the listbox and trying to get button clicked using VisualTreeHelper on Grid's MouseLeftButtonDown/Up, but those are never invoked when I click on the button.
I'm thinking that only possible solution would be using some kind of commands pattern when I register event handler in some global object and then call it from the custom control inside the DataTemplate.
I'm out of the ideas and hope someone had something similar to this issue.
Thank you.
Update
Thanks to McGamagle and ChrisW I got it working. The final code I have on the button looks like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=local:ListBoxExt}}" MethodName="PlayButton_Click"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Thank you guys!