处理由代码隐藏中的XamlReader.Load(xaml)加载的数据模板中的事件

时间:2016-01-10 11:35:16

标签: c# xaml winrt-xaml datatemplate

如何在代码隐藏中将事件绑定到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)
    { 
    }

我在这里做错了什么?

编辑:而且,我不想在这里使用行为,因为这些事件与我的数据无关。

1 个答案:

答案 0 :(得分:4)

哇,这比我想象的要多得多。但这是一个防弹,工作就像一个魅力。在此示例中,我正在处理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
}

祝你好运!