Handle an event in a Data Template loaded by XamlReader.Load(xaml) in code-behind

佐手、 提交于 2020-01-06 07:43:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!