For Karl: d:DataContext

89 views
Skip to first unread message

Laurent Bugnion, GalaSoft

unread,
Dec 6, 2009, 5:01:10 PM12/6/09
to wpf-di...@googlegroups.com

Hey Karl,

 

I have been playing with d:DataContext for a prototype I am building for a customer.

 

Here is the situation: I have some Views which are transient (in fact they are created by MEF, but that’s not relevant here). I set the DataContext on these temporary views programmatically.

 

One view represent an Account, returned by a service, with a contract of IAccount. In order to enable design, I thought I would create a DesignAccount : IAccount. In this class, I created some properties returning some design time value.

 

When I test, I see that the DesignAccount constructor gets called, so the d:DataContext works (in VS10 and in Blend 4). However the properties are never called. For example, my IAccount defines a FriendlyName of type string. In DesignAccount, FriendlyNamesimply returns “Hello”.

 

I try to bind a TextBlock.Text to {Binding FriendlyName}. My expectation was to see “Hello” on the design surface, allowing me to style it. However, nothing appears. In fact the property doesn’t even get called.

 

Re-reading your article at http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/, I get the feeling that the DesignAccount is really just a shape for the DataBinding editor, and that its properties are ignored. Am I right? If I am right, it is too bad, because we would have had the possibility to use these “faux types” for real design work. Instead I can only use that for the Data binding editor.

 

What am  missing my friend?

 

Cheers,

Laurent

--

Laurent Bugnion [Microsoft MVP, MCP]

Blog: http://blog.galasoft.ch

Web: http://www.galasoft.ch

Support children in Calcutta: http://www.calcutta-espoir.ch

 

cid:image001.png@01C9C8AA.B722DA80

My

business

card

as

Microsoft Tag

 

 

image001.png

Karl Shifflett

unread,
Dec 6, 2009, 8:14:39 PM12/6/09
to wpf-di...@googlegroups.com

Laurent,

 

Go back and re-read the section, Creating Creatable Types.

 

If you use this syntax the real type will be used.

 

<Grid d:DataContext="{d:DesignInstance local:Person, IsDesignTimeCreatable=True}">

 

If you still have a problem, send me a small example project.

 

Cheers,

 

Karl

image001.png

Laurent Bugnion

unread,
Dec 7, 2009, 4:49:32 AM12/7/09
to wpf-di...@googlegroups.com
Hey Karl,

Thanks, it works fine now. I had missed that.

Cheers,
Laurent
--
Sent from mobile
Blog: <http://www.galasoft.ch/> http://blog.galasoft.ch

Web: <http://www.galasoft.ch/> http://www.galasoft.ch

Support children in Calcutta: <http://www.calcutta-espoir.ch/>
http://www.calcutta-espoir.ch




cid:image0...@01C9C8AA.B722DA80

<http://galasoft.ch/contact/mobile/> My

<http://galasoft.ch/contact/mobile/> business

<http://galasoft.ch/contact/mobile/> card

as

<http://www.microsoft.com/tag> Microsoft Tag







<<image001.png>>



Laurent Bugnion, GalaSoft

unread,
Dec 7, 2009, 4:29:43 PM12/7/09
to wpf-di...@googlegroups.com
I am impressed by the d:DataContext support. My situation is as follows:

I have a WCF service returning a list of Customers. The contract is ICustomer. Since I use MEF, when I design my parts, I do not have any way to access the Customer proxy created by Studio.

So instead I added a ServiceContracts.Design assembly, which is only referenced at Design time (I tweaked the CSPROJ for that). Then, in this assembly, I define a DesignCustomer : ICustomer. I fill that with dummy data in the default constructor.

In my View, I set d:DesignContext to the DesignCustomer, so I can design against it. At runtime it is ignored, and in release configuration it is not even built nor deployed.

As Karl writes in his article, this is great for views where the DataContext gets set dynamically (these are typically transient views, views that have a short lifespan).

Very good work, guys.
Cheers,
Laurent

Corrado Cavalli

unread,
Dec 8, 2009, 5:42:00 AM12/8/09
to wpf-di...@googlegroups.com
Just a simple question: Isn't this approach (MEF+d:DataContext+tweaking etc...) more complicated compared to what you can do now with your toolkit?

Probably I'm still missing the key point but all MEF/MVVM samples have seen today give me nothing but overhead...

-Corrado

-----Original Message-----
From: Laurent Bugnion, GalaSoft [mailto:lau...@galasoft.ch]
Sent: lunedì 7 dicembre 2009 22:30
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] For Karl: d:DataContext

I am impressed by the d:DataContext support. My situation is as follows:

I have a WCF service returning a list of Customers. The contract is ICustomer. Since I use MEF, when I design my parts, I do not have any way to access the Customer proxy created by Studio.

So instead I added a ServiceContracts.Design assembly, which is only referenced at Design time (I tweaked the CSPROJ for that). Then, in this assembly, I define a DesignCustomer : ICustomer. I fill that with dummy data in the default constructor.

In my View, I set d:DesignContext to the DesignCustomer, so I can design against it. At runtime it is ignored, and in release configuration it is not even built nor deployed.

As Karl writes in his article, this is great for views where the DataContext gets set dynamically (these are typically transient views, views that have a short lifespan).

Very good work, guys.
Cheers,
Laurent

-----Original Message-----
From: Laurent Bugnion [mailto:lau...@galasoft.ch]
Sent: Monday, December 07, 2009 10:50 AM
To: wpf-di...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages