1

i need to clone a FrameworkELement in my CodeBehind in WinRT...

I did found a solution in the internet, though this workaround doesn't work in WinRT because the XamlWriter is NOT available in winRT! Is there an easy/built-in way to get an exact copy (clone) of a XAML element?

is there any other way to get another instance of my FrameworkElement?

Community
  • 1
  • 1
JuHwon
  • 2,033
  • 2
  • 34
  • 54

1 Answers1

2

I don't think there is an easy way to clone an element exactly - I don't know for example if there is a way to figure out the arbitrary attached properties set on one or figure out if properties are set by style, animation, template, explicit value etc.

There is one way that would possibly be a solution for your scenario if you have a specific element tree you want cloned - simply put it in a DataTemplate in XAML and then retrieve that template by name or resource key in code behind and call LoadContent() to generate an instance from the template.

If you have your original one in your XAML already that you don't want to put in resources and generate or lay out from code behind again - simply wrap it inside ContentControl/ContentTemplate/DataTemplate.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • im feeling like the biggest newb on earth! :D - my `FrameworkElement` i want to get an instance from is from an external library... so i'd need to create that datatemplate in my code behind.. I didnt found a solution how to do this in WinRT !??! is this even possible ? – JuHwon Feb 14 '13 at 21:35
  • I don't see why you'd need to do that in code behind. You can put it in your `ResourceDictionary` and get it from there. Otherwise - why not simply create it in code behind? Is the problem that you don't know how it is actually defined? – Filip Skakun Feb 14 '13 at 22:23
  • I get a `Collection` of Objects from an external lib containing a `FrameworkElement`. This `FrameworkElement` contains some panels including a canvas with individual XAML elements on it. So my status is that i got an `Instance` from a `FrameworkElement` and i dont know how to turn it in a `DataTemplate`in WinRT. – JuHwon Feb 14 '13 at 22:44
  • In that scenario my solution won't help you since the whole point of using a DataTemplate was to avoid having to create an element yourself. The only possibility is that maybe you can also get a DataTemplate that was originally used to generate the FrameworkElement that you got. What sort of library gives you a collection of FrameworkElement instances though??? – Filip Skakun Feb 14 '13 at 22:49
  • If it also gives you access to an `ItemsControl` (`ListBox`/`ListView`/`GridView`/`ComboBox` etc.) that generated these elements - you should be able to get the element by getting the `ItemTemplate`/`ItemTemplateSelector` of that `ItemsControl` and the `DataContext` of the `FrameworkElement` that's the item in the collection. – Filip Skakun Feb 14 '13 at 22:51
  • its an external lib from a client... so i can use things like these `FrameworkElement`s which they already implemented in their dektop application and i dont have to build them again.. thanks anyway! however i'll mark it as solved cuz your solution only doesnt work with my specific setup! :) – JuHwon Feb 15 '13 at 10:24