Nick
Nick

Reputation: 10499

Load element from external file

I want to load a WPF element from an external file. Example I have a file brush.xaml like this:

<SolidColorBrush>Black</SolidColorBrush>

And I want to load this brush in my code:

using(FileStream stream = new FileStream("brush.xaml"))
  Brush myBrush = XamlReader.Load(stream) as Brush;

How can I do?

Upvotes: 2

Views: 288

Answers (2)

gliderkite
gliderkite

Reputation: 8928

Your code is right, but in the xaml file use:

<SolidColorBrush xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
   #FF0000
</SolidColorBrush>

With the hexadecimal format. Anyway, if you have any doubt, you can make a test using XamlWriter.

Upvotes: 2

Justin
Justin

Reputation: 6549

Instead of loading it by parsing XAML. Put these elements in a resource dictionary. You can reference these resources in both XAML and C#.

For more info:

http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx

http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF

Accessing ResourceDictionary from WPF UserControl

Upvotes: 0

Related Questions