0

Usually, when I build my UWP applications, I put all my DataTemplate styles in a separate "Styles.xaml" file, to which the general ResourceDictionary in App.xaml is linked to through merging them.

A standard way that all professional developers use.

This time, however, I need to add a function to a certain event in a xaml control inside a DataTemplate, but I know the way of doing it only when the DataTemplate itself is defined inline (i.e. in the ItemTemplate property).

How can I, then, add some back-code to this event?

-> Please note that I already know data-binding an event to a function in a ViewModel and I already use it... this time I'm talking about adding some back-code that is not related to (i.e. contained in) the 'model' itself.

Thank you for your attention.

Best regards.

Luca Lindholm
  • 813
  • 1
  • 9
  • 23
  • It's impossible to achieve what you want.The XAML file is just a static resource file,you can't write a method in it without a .cs file or bind event from other page.The best way is to use data-binding to bind an event to a function in a ViewModel. – Faywang - MSFT Aug 16 '19 at 09:05
  • @Faywang-MSFT So, I guess, I’ll have to continue with inline DataTemplate definitions, on the same page where them are used. :( – Luca Lindholm Aug 16 '19 at 11:11

1 Answers1

0

You should be able to add a code-behind file to a XAML ResourceDictionary using the same approach as described for WPF applications here.

mm8
  • 163,881
  • 10
  • 57
  • 88