处理XamlReader.Load(xaml)加载的数据模板中的事件的代码隐藏 [英] Handle an event in a Data Template loaded by XamlReader.Load(xaml) in code-behind
本文介绍了处理XamlReader.Load(xaml)加载的数据模板中的事件的代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在代码隐藏中将事件绑定到 DataTemplate
(通过xamlreader生成)?
How can I bind an event to the DataTemplate
(which is generated via xamlreader) in codebehind?
这就是我尝试过的
string xaml = @"<DataTemplate " + namespaceString + " >" + rowsXaml + "</DataTemplate>";
Debug.WriteLine("Datatemplate is " + xaml);
try
{
var template = (DataTemplate)XamlReader.Load(xaml);
return template;
}
在XAML中:
<ListView
ItemsSource="{Binding Source={StaticResource GroupedRecords}}"
formatter:SwipeListHandler.RecordTemplate="{Binding RecordsTemplate}"
BorderBrush="Black" ></ListView>
附属财产:
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)
{
}
我在这里做错了什么?
编辑:而且,我不想在这里使用行为,因为那些事件与我无关数据。
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.
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
}
祝你好运!
这篇关于处理XamlReader.Load(xaml)加载的数据模板中的事件的代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文