Click here to Skip to main content
12,824,299 members (40,318 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML
Quick question:
Is is short work using XAML to have the types of controls configured/controlled by the underlying data. I see plenty of examples of configuration of a control's look/feel based on data content; I am looking to base the control type on the associated data/properties.

Examples include:
. Value returns Bool object, the control should be a button
. Value returns Int32 object, the control should be a textbox
. The data could expose a property, Widget, that would specify button, textbox, etc

I suppose one could write this in C# pretty readily or cobble together a ContentControl that contains all possible button, textbox, etc combination and the data could control which are visible but these seem such hacks. I thought I would ask the experts if a more natural xaml solution exists.
Posted 17-Dec-12 7:18am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Depending on the situation, one possibility could be to build a custom control that handles 'individual controls'. This custom control would adapt to the data type etc.

Another option could be to use a property grid such as Wpf Toolkit - PropertyGrid[^] or for example Native WPF 4 PropertyGrid[^]
Espen Harlinn 17-Dec-12 12:55pm
Nice idea :-D
Mika Wendelius 17-Dec-12 14:20pm
Thanks Espen, even though it's seems that it's not what OP is after :)
Espen Harlinn 17-Dec-12 15:34pm
Then he is probably looking for information on the DataTemplateSelector
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

If the PropertyGrid isn't what you're looking for then I guess you're looking for something like WPF Based Dynamic DataTemplateSelector[^]

Best regards
Espen Harlinn
Tall Bill Clark 17-Dec-12 17:27pm
Nail hit on on head. This is very much what I was looking for.

I may have skimmed thru this before but dismissed it when I saw the size of the code involved thinking "this *has* to be easier". I can accept that it isn't easier. Even the write up allows that this looks burdonsome but still pays off when there are many types that need to be matched and managed. Thanks!
Espen Harlinn 17-Dec-12 17:30pm
Brilliant :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100