问题
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.
回答1:
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.
public MainPage()
{
InitializeComponent();
Instance = this;
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs args)
{
var xaml = " <DataTemplate xmlns:local=\"using:App1\" "
+ " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"> "
+ " <Grid Margin=\"4\" Width=\"150\" Height=\"100\" "
+ " local:MainPage.CustomLoaded=\"true\"> "
+ " <Grid.Background> "
+ " <SolidColorBrush Color=\"{Binding}\" /> "
+ " </Grid.Background> "
+ " </Grid> "
+ " </DataTemplate> ";
MyItemsControl.ItemTemplate = XamlReader.Load(xaml) as DataTemplate;
MyItemsControl.ItemsSource = new[] { Colors.Red, Colors.Green, Colors.Blue, Colors.Orange, Colors.Purple };
}
private static MainPage Instance { get; set; }
public static bool GetCustomLoaded(DependencyObject obj) { return (bool)obj.GetValue(CustomLoadedProperty); }
public static void SetCustomLoaded(DependencyObject obj, bool value) { obj.SetValue(CustomLoadedProperty, value); }
public static readonly DependencyProperty CustomLoadedProperty =
DependencyProperty.RegisterAttached("CustomLoaded", typeof(bool),
typeof(MainPage), new PropertyMetadata(null, (d, e) => Instance.GridLoaded((d as Grid))));
private void GridLoaded(Grid grid)
{
grid.ManipulationMode = ManipulationModes.All;
grid.ManipulationDelta += Grid_ManipulationDelta;
}
private void Grid_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// TODO
}
Best of luck!
来源:https://stackoverflow.com/questions/34704890/handle-an-event-in-a-data-template-loaded-by-xamlreader-loadxaml-in-code-behin