views:

367

answers:

3

Hi,

I am big into code generation for the service/data layer of my apps. What I would really love to do is generate some basic WPF Controls, Data Templates, or some other XAML code based on the metadata I use to generate my service/data layer. EDIT: This generation is done before compile time.

What I envision is being able to generate a control which has basic controls in it TextBlocks, Labels, Date Pickers, Textboxes, Checkboxes, etc based on types of my data.

The big thing I am missing is to somehow never touch that generated XAML code and control the layout completely outside of it. In the web world I could technically do this by generating "semantic" html and then using CSS selectors to select nodes and position them. With CSS/HTML I could completely change the layout and never touch the generated html.

Is there someway in WPF to control layout of a Control or Data Template from outside of that code? (Selectors, Visual Inheritance, etc?)

Thanks! Jon

A: 

If I understand correctly, you want to generate XAML dynamically, then parse it and use it?

If so, you can parse/load it into memory using System.Windows.Application.LoadComponent(Uri uri). OR you can use XamlReader.Load(...).

Edit (read the question again, so adding some things): You can use WPF Styles to position and control layout of those elements.

siz
+2  A: 

Hi,

Use Grid.SetColumn( UIElement, value ) & Grid.SetRow( UIElement, value )

Sauron
Thanks Ortus! Been looking for this for about 30 min.
Brad
A: 

You can change Styles (and even ControlTemplates) in WPF-Dialogs by using Resources: If the Resources (at any level, for example the Application) contain a Style with TargetType=TextBox, it will affect all TextBoxes within the Control that do not explicitly state a deviating Style. Alternatively you can tell your UserControl to use certain Style-Keys (StaticResource), and then later decide, what DataTemplate you plug in for these Style-Keys. Just choose DataTemplate-ResourceFiles where the DataTemplates use the right keys and merge them into your Application-Resources. If you change the Resources this way, your generated code will respect these changes at runtime.

You could even change the Resources at runtime, but your question reads as if this was not necessary.

Simpzon