Simple XAML viewer in WPF - wpf

I need to write XAML of a WPF control to string. I used XamlWriter.Save(). It works good. Also I need to format, indent and show the string in a editor (read only) like VS Xaml code view. I dont know how to do this.
Please share your valuable ideas.

You can save to a XmlWriter instance, create a XmlWriter, create a XmlWriterSettings instance and set the Indent property to true and set this on your XmlWriter. That way you should have the indentation.
Now for a control to display it, you could use the RichTextBox in wpf, or look for a custom control around the web.

Related

Setting default values for inherited properties on a custom control in WPF

I'm trying to subclass a charting tool that has on it an XAxis property, which is of type IAxis (so you can give it a NumericAxis, DateAxis, etc). When using the base chart normally, I can set the XAxis like so:
<chart:BaseChart>
<chart:BaseChart.XAxis>
<chart:NumericAxis VisibleRange="{Binding XAxisVisibleRange}" AxisAlignment="Bottom"/>
</chart:BaseChart.XAxis>
</chart:BaseChart>
However I'm not really sure what the best practice is for setting this default axis in a custom control, should it be in the XAML, in the C# code-behind, etc? I can get it working from the code-behind by setting the XAxis property/bindings in the constructor of my subclass. This works, I just don't know if it's the "correct" way to do it.
Being new to WPF, I don't know where/how to do it in XAML for the custom control, if that's even the right place for it to be. Should it go in the control template, the style, somewhere else? I tried doing a Setter in the style, that did not work (it compiled but I did not see any axes). From the control template, it doesn't recognize <chart:BaseChart.XAxis>. There are AxisAreas defined for top bottom right and left, and if I simply put the <chart:NumericAxis etc/> inside one of those then the axis does show up there, however that doesn't feel correct since it's not actually setting the XAxis property on the BaseChart, plus it's bypassing the AxisAlignment property on the NumericAxis object and explicitly placing the axis in whichever AxisArea I put the axis code in.
Like I said, I do have this working from the code-behind, I just want to make sure I'm doing it the best way I can. Any help would be much appreciated, thanks in advance!
Please take a look at the Control Authoring Overview page on MSDN for a complete answer to your question.
However, in short, for a CustomControl, you should create a folder in your application root directory named Themes. You should add a XAML file named Generic.xaml into this folder and you should put a ControlTemplate for your CustomControl in there.
For a UserControl, you can just add your Styles into the UserControl.Resources section of the page.

WPF - RichTextEditor

im searching for a Usercontrol that generates XAML-Code (Flowdocument Code)
For a Bold Text -> <Paragraph><Bold>Text</Bold></Paragraph>
You may want to have a look at the RichTextBox in the Extended WPF Toolkit - http://wpftoolkit.codeplex.com/wikipage?title=RichTextBox
I haven't tried it, but according to the documentation it has a XamlFormatter as well.
From what kind of object do you want to generate the Xaml-code?
If it is a TextBox content you could try to use some Converter classes.

Is WPF's FlowDocument content user-editable?

I understand and have read about using WPF's FlowDocument to create an XML style document on screen, but is the content presented editable by the user, or is it read only? And, if so, how is this done?
My question mostly centres around the use of the listitem control because it would be nice to be able to edit the order of the list items presented for use in my program, rather than me having to create my own custom control(s).
Regards.
FlowDocuments as objects are editable, if they can be edited in the UI depends on the controls that use them. If you use them in a Page they are not, if you use them in a RichTextBox they are.

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.

Resources