Can a UserControl be saved as XAML at runtime in WPF? - wpf

I have a UserControl that includes Labels, TextBox and Charts (generic types). After the user loads this control in a grid at runtime, can it be saved as XAML? So a later user can load his file and edit it. If the UserControl cannot be saved as XAML what is the other way to save it?
Thanks for any inputs,

I think you will want to look at the XamlWriter / XamlReader Classes. There are some limitations but they might work for you.

Related

wpf binding text

WPF C# VS2008 3.5 I have a few pages(page1.xaml ect.) and I would like to bind text from a textbox to a label on a different page in xaml. I tried using ElementName but the label does not show the text user enters in textbox. I know something is wrong in the Path, but I'm new to wpf so any help would be great. My xaml referenced the x:Name of the textbox, should I reference the page also? thanks in advance
I think it would be better to create a class that will hold the data and create a single object of that class and bind both pages to it.

What is the best approach to load xaml directly?

I have xaml directly stored in database. Mostly they contain hyperlinks and textblocks. How do i load this in xaml such that the controls are instantiated and seen properly. If i bind it directly to textblock the text will be seen as it is (i.e the xaml won't get instantiated). One approach is to use richtextbox and it would work fine. However, i want a label type of control and not a textbox.
Thanks in advance :)
A good approach here is to use XamlReader.Load() (Silverlight) or XamlReader.Parse() (WPF) which parses a string and returns an element corresponding to the root of the XAML document.
XamlReader.Load Method (String)
XamlReader.Parse Method (String)

Dynamic controls in WPF MVVM

I have an app with MVVM which works fine. Now I want to replace one of my controls with a dynamic control. By dynamic I mean that I have no idea what control this is, only that it is a GUI control. It could be something as simple as a image, or a custom third party user control that will be created by someone else after this app is done.
Can someone shed some light on how this can be achieved in MVVM? I've done it before a long time ago using ListBox or similar (iirc) to generate GUI elements (don't remember details). But I'd like to learn the theory behind it this time.
Edit:
Lets say the View contains a list of instances of for example System.Windows.UIElement. I want to display all of these UI controls on a surface (for instance in a stacked control).
You could create a View that exposes a Content property as a placeholder (so a ContentControl might be all that is needed) The content property could then be set to the dynamic control.
You would have to add a little reflection to dynamically load the assembly and instantiate the required control.
The dynamically loaded control would have to access the data by using the DataContext property. If the dynamic control is MVVM too it might have its own ViewModel so you would have to find a way to load that too (reflexction again?) and point the DataContext of the control to the loaded ViewModel.
Does this make sense, is this what you are looking for?

How I can Save layout in Silverlight

I am working on silverlight application and I want save layout as a sheet.
The flow is here.
I Have StackPanel and children of StackPanel is WrapPanel and inside the WrapPanel I am adding multiple Images.
The Size of the StackPanel is according to user define height and width.
so how I can save this sheet.
Thanks..!!!!
In WPF there exists a XamlWriter which can write your Visual Tree to a XAML string. However, this is not available in the Silverlight APIs. Fortunately someone has written a XamlWriter for Silverlight and it is available here:
http://www.davidpoll.com/2010/07/25/to-xaml-with-love-an-experiment-with-xaml-serialization-in-silverlight/
With this you can save your 'sheet' to a XAML string, the use XamlReader.Load to reconstruct it:
http://msdn.microsoft.com/en-us/library/cc189076%28VS.95%29.aspx

Difference between Control Template and Data Template in wpf

Can someone elaborate the difference between ControlTemplate and DataTemplate in wpf?
What should one use in case of custom controls? Like for example a StackPanel which possibly has an image and a TextBox?
It seems confusing in some cases where you define a custom control using the 'Content' property.
It would be great if an example of how each can be used in different scenarios can be provided.
A ControlTemplate is used to change the look of an existing control. So if you don't want your buttons to look rectangular, you can define a control Template which makes them look oval or any irregular shape. It's a way to customize 'look-less' stock WPF controls ; an alternative to writing your own user-controls. More details
A DataTemplate is used to specify how an instance of a specific class (usually a Data Transfer object - an object with properties) is to be rendered visually. e.g. define a DataTemplate to visualize a Customer instance in a listbox displaying all customers. More details

Resources