Get XAML source of WPF Window - wpf

I would like to get the XAML source of a WPF Window (MainWindow). Clicking on a button on that window would return the XAML of the window and I would save it in another file.
Is this possible and how can it be achieved?

You can use the XamlWriter:
using (Stream stream = File.OpenWrite("D:\\Test.xaml"))
{
XamlWriter.Save(this, stream);
}

You can use XamlWriter for some basic Xaml Serialization.
In particular, look at this article on its limitations.

The earlier answers are both correct, but I think it should also be mentioned that you can also extract the original XAML used to create the window (if desired) using the API for Reflector's BAMLViewer extension.
BAMLViewer solves a different problem than XamlWriter: Using Reflector / BAMLViewer will return the original source XAML with all bindings, etc intact but will not include current property values. Using XamlWriter will include current property values but such things as resource references and markup extensions will be lost. Also, some things will not serialize using XamlWriter.
You must choose between these based on your application needs.

Related

XAML Resx localization not working as expected

I'm attempting to use a resx file to localize some strings I am using in a XAML file. I've looked around at other documentation on the web, and they all seem to recommend a two part process:
Add a clr-namespace to your window, like this:
xmlns:props="clr-namespace:PJConfiguration.Properties"
Use that namespace to localize your string like this:
Content="{x:Static props:Resources.SharedSettings}"
I've done this, and also made sure that my resource classes are public, but I still get the following error from the XAML in step 2:
Cannot find the type 'Resources'.
Does anyone know what else might be causing this problem? Thanks in advance.
In order to make the Resources visible to XAML, you have to make sure that the code generation mode for the resources is set to public. In VS, you find that setting in a ComboBox near the top of the Resources designer window.
For more information on using .Net resources in XAML, you might want to refer to these blog posts: http://wpfglue.wordpress.com/category/localization/
Check if your .resx file is the default Resources.resx file inside the Properties directory of the Application assembly. If that is, there is no reason XAML couldn't find the public class Resources from the correct namespace under local assembly.
Try to specify the assembly name in Step 2 as recommended in this answer.

Can I create equivalent of C# attribute or metadata for WPF child controls in XAML code?

Can I add some kind of custom attributes in XAML ?
Perhaps you mean Attached properties (scroll down for info on how to create a custom one)? For examle "DockPanel.Dock" is an Attached property.
Or maybe Markup extensions? (though that is something a little bit different)
You can also place Attributes on custom controls and read them in code behind - just like any other .NET class, but that is not possible to do from XAML (since those are set per-type/method/..., not per-instance).
You can, using Attatched Properties. This lets you add your own and data to the xaml on any control you choose.

Can not get image information from Silverlight RichTextEditor

I'm trying to use the Silverlight RichTextEditor in our website. Now we'd like to translate the content in richtextbox into HTML code to save and load.
However, as we know, the richtextbox control does not support the UIelements output. When we insert an image in the richtextbox, the richtextbox would use a inlineUIcontainer to show this image. The property Richtextbox.xaml does not include any information about the image. It just shows the code like "".
Does anyone have this problem and handle it before?
RichTextBox.Xaml strips out a lot of things, as a security safeguard (more for the setter than the getter as far as I recall, but it does it both ways so there are no round-trip surpises).
I recommend looking at the XAML Serializer written by David Poll on his blog (here: http://www.davidpoll.com/2010/07/25/to-xaml-with-love-an-experiment-with-xaml-serialization-in-silverlight/ ) as it can serialize RTB awesomely well (it's in fact one of the test cases he shows). David was a PM on the Silverlight XAML Parser in SL4, so he knows an awful lot about XAML.
But be careful when setting the .Xaml property, as you could mistakenly end up spinning up InlineUIContainer elements which load resources into your AppDomain that you don't want in there, so make sure you control the inputs or you strip them yourself very carefully.

Supporting additional Uri Schemas for the Image.Source property

Is there a way to make the following code behind expression to evaluate to true given the Xaml below. I'm asking this question because I have to work with a Library that converts FlowDocuments containing Images to HTML. Only the "Source" Attribute of the Image Control is persisted and you can't use the standard approach of using a BitmapImage because this won't convert back to a Uri String.
Debug.Assert(this.image1.Source.ToString() == "data:image/x-png .. <rest of the Image.Source Property below>")
<Image x:Name="image1" Source="data:image/x-png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAy1JREFUeNpMU1toVFcUXefc18zNZJJJZmLHSDAmCiqKrbV/7U8Rv5oPQT9URKgobUVQQRD88cOfVq20NNZGSotIqQpaTXzGiM+KQWK0kvEVw0xGzENm4sxkzr3nnHt77jRT3HDhcs7ea+219j7E930Ekc1m8ezFMJrq7E88I/RwYmzcHR3NQNM0xGIxNCRmQWfvVuqRukFHyPGR4WEMDQ2BYiaIZoBI3jx2/seu0suHm6gVrpwLIeCBQIeck/nz8JFc/7XdeqimWga9WCyqagopeEO+53B3/t75peEHfT8n1u0rw4wcB3dACYmnf9/fk+071/b6bu+u+RJMa164NwDQ5rW14emTfxK5cwe7iwNXP7YjddCdIhGpvzt4Y+tj124YMXuPXZi+f2mFGY6Ac4nJ/hufUTuqvwvHrpO29nbYvtO6sWnyTnuDndQUqkUJ4LoQVnS6HEmmrPSTjwQx4HCg5HooMo7jo+43I1ZjJ6lqqbfo3D3La7uXx43FxAMMKHOFB58LuL4Ol/tg3EOeeeh8xbY8mBJdFQlVACb9/K2M89eiWrmqJWI2UVWvK/OIrzgUoCeBQpm73w+Vtg+UvKP/m9/R0VH5kVLizfgkklGr5SszNRgDrycega8KXcXsMIF+fdbpi+H2NaKQr+QXCgVFMLMHQVy42gd658RRevv0ZotqVFNXVYASk+BaiGmrv975Ot56JP0shXQ6DT2XywWNYFpITPX+dsi4fXJLjdoBJVmxqCvPr5gnlZRyoRgq/Hqg0/l8rXBqP+jinEMrM4YbN29ambOd3xkDl3fouqloidoLwBE+skU5YQE1TCG6ShJzlalPH38hqc7ydv1dzZUeRl+kmhZMPPolahlh1YgyzEcwiVMZ9tMPw2zDHIt+2qiR5lIAEpiqah69ykw85/QMsW0b1LAQcaaW7VhoX4kbNEFVce+Y88eJjLNeifDDFPGts60rrRb9UCjwewVx5tRbsTbYdLwfyRBd9u3SyOSXc0M97484iBBFYlvSHNyUMPpIsCbVMaoveFBKOCqvJ6qTJWXpZ1S3WfyXGOQEdnKToEU1Zypr3s6wl/8VYACfeYRluTSjlQAAAABJRU5ErkJggg=="></Image>
The solution is to implement a pluggable protocol for the "data" Scheme. This way the Source property acts exactly as with the built-in protocols for Image retrieval. The technique is described here.

Modify XAML string dynamically

I want to add/remove some part of XAML dynamically i.e. from code behind file in C#.how to Add any specific XAML string on specified location (means under some tag) from .cs file. Please help.
XAML is xml you can use XmlReader and XmlWriter or any other of the XML serialization mechanismn of .NET to write/read XML (XMlDocument is probably the best approach here to modify existing xml). ALso there are the XamlReader and XamlWriter class that allow ou to (de)serialize any object graph from/to XAML automatically. If you can use .NET 4.0, you have even more fine grained possibilities to athor XAML because it has a new XAML stack. Use this as a starting point.
What is it that modifying the XAML will do for you?
If you just want to change the appearance of your WPF application (perhaps by adding some more content at certain locations), it will most likely be easier to do this by referencing the objects in question. So, if you need to add some text to a button, name the button with x:Name="myButton" and in code set: myButton.Content = "Click Me"
XAML is really a technology for constructing object hierarchies. Pretty much every element in the XAML corresponds to a .NET CLR class. When loaded, these classes are instantiated nd populated according to the attributes used in the XAML. Once loaded, the XAML has finished it's job and is essentially unloaded/unavailable.
You might need to do something beyond this, but from your brief question it doesn't seem like it. I would just work on the object model and leave the XAML be.

Resources