How can I bind an event to the DataTemplate
(which is generated via xamlreader) in codebehind?
This is What I've tried
string xaml = @"<DataTemplate " + namespaceString + " >" + rowsXaml + "</DataTemplate>";
Debug.WriteLine("Datatemplate is " + xaml);
try
{
var template = (DataTemplate)XamlReader.Load(xaml);
return template;
}
In XAML:
<ListView
ItemsSource="{Binding Source={StaticResource GroupedRecords}}"
formatter:SwipeListHandler.RecordTemplate="{Binding RecordsTemplate}"
BorderBrush="Black" ></ListView>
Attached Property:
private static void RecordTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListView view = d as ListView;
DataTemplate template = (DataTemplate)e.NewValue;
var val = template.LoadContent();
Grid border = XamlHelper.FindElementByName<Grid>(val, "RecordItemStackPanel");
if (border != null)
{
border.ManipulationDelta += border_ManipulationDelta;
border.ManipulationCompleted += border_ManipulationCompleted;
}
view.ItemTemplate = template;
}
static void border_ManipulationCompleted(object sender, Windows.UI.Xaml.Input.ManipulationCompletedRoutedEventArgs e)
{
}
static void border_ManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e)
{
}
What I'm doing wrong here?
EDIT : And, I dont want to use behaviors here, since those events have nothing to do with my data.
Wow, this is more code than I imagined it would take. But this is bullet proof and works like a charm. In this sample, I am handling Manipulation events, but you could do anything at this point.
Best of luck!