views:

910

answers:

2

I love the feature in Blend where you can setup databinding to CLR Objects, and design your Silverlight control/page. My issue is around the creation of a user control that will be used as a DataTemplate. It all works great in Blend, but only at design time. The issue is that when I am editing the control in Blend, and I bind say a textbox to the property of a CLR object, it gives me this:

Text="{Binding Mode=OneWay, Path=TestAccount.Name, Source={StaticResource TestDataDS}}"

But when I am ready to run the app, and use that user control inside a DataTemplate for say a list box, I need it to just look like this:

Text="{Binding Mode=OneWay, Path=Name}"

How can I use the nifty binding to design in Belnd, but not have to touch/search&replace all these bindings when I am ready to run the project? I am looking for an approach that will work both in design time and at run time.

Thanks,

A: 

In order to create a data binding to a CLR object in Expression Blend, you don't need to use the "DataField" tab of the "Create Data Binding" dialog. In fact, doing it this way is very rare.

Using a custom path expression is the most common way. Simply checking the "Use a custom path expression" check box and then typing in the name of the property that you would like to bind to. If you leave the data context unspecified, then the data binding will resolve to the path that you specified on whatever object happens to be the DataContext.

If you want to set a design time DataContext you can do that by setting the d:DataContext property, making sure to properly define xmlns:d="..."

markti
I guess I am not following you. I created a simple class called TestData which I use to return hydrated objects from my project. For example an account object with a name property on it. So in the code I pasted above, You see real time while you are in Expression Blend, and you bind a TextBlock Text property to Path={MyObject}.{Property}, Blend reaches into the object and grabs the value off of that hydrated object. So when you view the designer, you see the textblock with text in it. That lets you play with the layout to see how your control would look. But the binding syntax is only good...
for when you are inside blend. When I am at run time, the binding is no longer valid because I don't want it bound directly to a test object, I want it bound to the object in the DataContext. So the first code snippet works when in Blend, but the second code snippet works at run time. What I am hoping for is a solution that will give me both.
now i understand, you want DesignTime bindings AND runtime bindings, not just how to set the binding that works at runtime from within blend.
markti
Exactly! I may be lazy, but it sure is nice to have data at design time in Blend (think data templates), and not have to change the code at run time.
A: 

If you're using blend 2, I'd recommend you take a look at Jonas Follesoe's design time / run time blog post which uses dependency injection.

If you're lucky enough to be using Blend 3, then check this out

When you're setting bindings up in blend, is the "Explicit Data Context" tab available?

Rob Fonseca-Ensor