locked
Is it possible to add Event Handler in separate ResourceDictionary directly?

    Question

  • Hi! Traditionally, if we want to handle events in a DataTemplate which are put in a separate ResourceDictionary file, we use Command binding or Behavior. But here is the thing: First, Command binding method seems quite limited, since many FrameworkElments don't have a Command dependency property; Second, based on my experience, it seems that Behaviors can introduce memory leak easily.

    So just wonder if we could just create a code-behind file manually and handle events there. I know someone has done this in WPF successfully(http://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handling), but I just can't get the method work in Windows Store apps projects. My quetion is: how to use this method correctly in WinRT project? If this method can't work, is there any other method to do this? Thanks!



    Thursday, February 26, 2015 4:05 AM

All replies

  • check this code sample command binding in DataTemplate. https://code.msdn.microsoft.com/windowsapps/Command-binding-inside-3cef5eea
    Friday, February 27, 2015 5:12 AM
  • check this code sample command binding in DataTemplate. https://code.msdn.microsoft.com/windowsapps/Command-binding-inside-3cef5eea

    As is said in my post, Command binding method is quite limited, since many FrameworkElments don't have a Command dependency property, so they can not use Command binding. But thanks!
    Friday, February 27, 2015 5:25 AM
  • Hi Silverbird2015

    >> Is it possible to add Event Handler in separate ResourceDictionary directly?

    According to your description, I assume you want to add event handler in DataTemplate in Resource Dictionary at runtime. It seems you are making thing more complex. Are you want to separate the Data and UI, if so, I suggest you take a look at MVVM pattern in windows store app. https://msdn.microsoft.com/en-us/magazine/dn189204.aspx.

    If I have any misunderstanding, please post more information about your scenario.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, March 1, 2015 6:16 AM
    Moderator
  • Hi Silverbird2015

    >> Is it possible to add Event Handler in separate ResourceDictionary directly?

    According to your description, I assume you want to add event handler in DataTemplate in Resource Dictionary at runtime. It seems you are making thing more complex. Are you want to separate the Data and UI, if so, I suggest you take a look at MVVM pattern in windows store app. https://msdn.microsoft.com/en-us/magazine/dn189204.aspx.

    If I have any misunderstanding, please post more information about your scenario.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thanks! My scenario is: my project is almost done and I want to do some performance optimization. There is an important page which is put on a Popup Control and will be created very frequently by users. This page has a very complicated DataTemplate in its Resources(Xaml), so in order to avoid recreating the DataTemplate each time when the page is recreated, I've to move it to App.xaml file. But the DataTemplate has some event handler in it, and these event handlers can't be replaced by Command Bindings, and I don't want use Behaviors either. So I've got a big problem and stucked here.

    btw: The project is almost finished, so it's not economical to introduce a complete MVVM FrameWork for just departing a DataTemplate.


    Sunday, March 1, 2015 6:47 AM