1

After reading into XamlWriter and XamlReader, I was wondering if it is possible to load an arbitrary .xaml file at runtime, edit the object tree and then reuse it again.

What I am trying to create is a kind of showcase application for all available styles of a project. I've got TemplateViews for several ControlTypes (such as ButtonTemplateView.xaml, ListboxTemplateView.xaml ...), with their Style property bound to a viewmodel, which get dynamically constructed for each fitting style at runtime and then added to the main view. But I also want to show all styles for the CustomControls of the project, which right now I am doing via Activator.CreatInstance with the TargetTypeof the style, and then adding the object to the main view.

Now, lets say if a CustomControl MyCustomTextBox is based on a TextBox, can I just load the TextBoxTemplateView.xaml, switch every <TextBox ... /> to a <MyCustomTextBox .../> and then add it to my main view?
If so, how? Can I turn it into a string and just replace the words and then turn it back into something usable? Or do I have to edit the UserControl object I get when I use XamlReader.Load? Or something else?

I hope this is not a duplicate question (at least I didnt find anything like it) and thanks for any help in advance.

tobi_fis
  • 55
  • 6
  • Why don't you simply try it? – Clemens Jun 27 '17 at 08:26
  • If I knew how, I would... Why do you think I came here? – tobi_fis Jun 27 '17 at 08:45
  • 1
    "switch every to a " sounds like a simple string replacement before loading the XAML. Have you tried anything at all? – Clemens Jun 27 '17 at 08:47
  • No I havent, because I don't know where to start. Can I edit the file directly? Can I turn it into a string and just replace the words and then turn it back into something usable? Or do I have to edit the `UserControl` object I get when I use `XamlReader.Load`? Or something else? – tobi_fis Jun 27 '17 at 08:59
  • See here for an example how to load XAML from string: https://msdn.microsoft.com/en-us/library/ms590388(v=vs.110).aspx#Examples – Clemens Jun 27 '17 at 09:05
  • Thanks, but I have read this page already and it doesnt answer my question. This will just return a UserControl Object, but then what do I do with it? To be clear, I want the properties of the new control to be the same, I just want to replace the ControlType. – tobi_fis Jun 27 '17 at 09:10

1 Answers1

1

You could either replace all occurances of <TextBox> with <local:MyCustomTextBox> in the string that you pass to the XamlReader.Load method. This is probably the easiest way because then the XamlReader will create the MyCustomTextBox elements for you.

The other option would be to iterate through the <TextBox> elements in the UserControl that is returned from the XamlReader.Load method and replace these by MyCustomTextBox elements. How to do this depends on where the controls are located in the element tree of the UserControl.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks a lot! I tried the first approach, the problem is, how can I turn the .xaml file into a string? I tried to do it with `GetManifestResourceStream`, but then I'd have to mark the file as embedded resource which makes it unusable. Is there a way to get a string out of my file and still use it in the project? – tobi_fis Jun 27 '17 at 10:01
  • What's the build action of the file? – mm8 Jun 27 '17 at 10:03
  • Page, it's a UserControl. – tobi_fis Jun 27 '17 at 10:07
  • Then it is being compiled into BAML so there is no string to be found. If you set its Build Action to Output you can read the file as-is. The XamlReader class works with XAML, not BAML. – mm8 Jun 27 '17 at 10:08
  • I dont have the option to set the Build Action to Output. I there any other way to get the xaml string from the usercontrol? – tobi_fis Jun 27 '17 at 10:15
  • Well, then I don't know why you are trying to parse the "file" because there is no file after the application has been built. – mm8 Jun 27 '17 at 10:16
  • yeah thats why i asked if theres any other way to get the xaml as a string – tobi_fis Jun 27 '17 at 10:38
  • 1
    Once again, there is no XAML at runtime. So the answer to that question is no. – mm8 Jun 27 '17 at 10:41