What is the best approach to load xaml directly? - wpf

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)

Related

Can a UserControl be saved as XAML at runtime in 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.

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.

How to Bind Grid with Properties in Code behind?

I have a grid in a XAML file and I want to generate its RowDefinition dynamically. To do that I create a List<RowDefinition> in which I add 3-4 RowDefinitions. Now I have to bind this property to the grid in the XAML file. How do I do this?
If you want to host dynamic content (varying number of items), the Grid control might not be your best option. Try using an ItemsControl (which can be templated to look however you want) and bind your actual data against the ItemsSource property. If you post some more information about what kind of content you are looking to display I can give you an example of how to do it with an ItemsControl.

Silverlight: how to display multiple properties of an object in a ListBox using DisplayMemberPath

I need to do this in code, not XAML.
Its not possible to programmatically create the content of a data template in the same way you might create Controls and add them to a UserControl.
Instead you will need to use System.Xml.Linq.XDocument to construct the DataTemplate as XML with DataTemplate being the root element. Once complete you can retrieve the XML string for the Root element and then use XamlReader.Load to get a constructed DataTemplate.
With a ListBox, you can specify a DataItemTemplate - just put whatever controls you want in that DataItemTemplate (e.g. you might want multiple TextBlocks), and bind them to whatever properties you want on the bound object.
To load the DataItemTemplate dynamically, you can use the same technique as illustrated in this blog post.

WPF bind to a list

I have a user control whose users I want to have them set a DataContext on to bind to a list of objects. In my control, however, I want to display that list in a Grid, but in a non-trivial order. The column/row of display of each element will be determined by some code I will write.
So I cannot do a straight databinding in my control, I need to write code that will read the DataContext and then do the processing to correctly position each element.
How would a relative WPF newbie go about doing that? I guess the part I don't understand is what the code in my usercontrol will look like to read the DataContext items so that I can process them.
EDIT: Clarification: I want to stress I want to bind to the XAML Grid element, not some other kind of grid or DataGrid. Thx!
One of the possible way to achieve that is to use a Converter. You could create a Converter which converts the input list into another list where the order has been changed. Then you could use a "normal" databinding which will use the Converter.

Resources