在代码隐藏中处理由 XamlReader.Load(xaml) 加载的数据模板中的事件
Handle an event in a Data Template loaded by XamlReader.Load(xaml) in code-behind
如何在代码隐藏中将事件绑定到 DataTemplate
(通过 xamlreader 生成)?
这是我试过的
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)
{
}
我哪里做错了?
编辑:而且,我不想在这里使用行为,因为这些事件与我的数据无关。
哇,这比我想象的要多。但这是防弹的,并且很有魅力。在此示例中,我正在处理 Manipulation 事件,但此时您可以执行任何操作。
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
}
祝你好运!
如何在代码隐藏中将事件绑定到 DataTemplate
(通过 xamlreader 生成)?
这是我试过的
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)
{
}
我哪里做错了?
编辑:而且,我不想在这里使用行为,因为这些事件与我的数据无关。
哇,这比我想象的要多。但这是防弹的,并且很有魅力。在此示例中,我正在处理 Manipulation 事件,但此时您可以执行任何操作。
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
}
祝你好运!