aurath
aurath

Reputation: 1407

VS Design Time DataContext (Sample Data) Contains Exception

Application

It's a WPF application meant to send files between clients. I'm working on a usercontrol meant to show a list of sent files, their recipients, etc.

Setup

I'm trying to style the control, but I need sample data to populate it. Following these instructions, I create a SampleFilesSource.cs that extends DataSourceProvider. It generates an instance of the view's viewmodel populated with an ObservableCollection of random recent files.

Next, in the view, I set the design time DataContext with this:

<d:UserControl.DataContext>
    <sample:SampleFilesSource/>
</d:UserControl.DataContext>

But nothing shows up... What happened?

The Data-Bound Exception

Well, let's see. The Data pane in Blend allows us to see what's in the design time DataContext that's available for binding. Let's take a peek.

Exception inside DataContext

Well that's interesting. There's an exception in my DataContext. The Data pane doesn't show you any of the values, just what fields can be bound to. So how do I get a peek at the exception's Message? Can I bind to it?

Binding To It

Unfortunately not... In fact the only binding I can get to output any text is "(Collection)", when I try {Binding Data.RecentFiles}. The fields are visible under the Data object, but nothing has any values.

But what if I set the run-time DataContext to the sample data generator? Just remove that little d: from the DataContext tag and debug... Still no luck. By taking a peek at the live visual tree I can see the DataContext is bound to an instance of the viewmodel. And interestingly enough, the only thing in the datacontext this time is an Error field.

There's no Data field at run-time and the Error field is an instance of Exception. I think it's null. The property explorer just has an empty box, with no options to view it. I think it's weird that no unhandled exceptions showed up in the debugging session. It got stuck in the DataContext somehow?

Debugging

So other than clean, rebuild, restart VS, restart the computer, and whatever else I had already tried, what else can I do to get a peek at that Message? Here's a handy little guide to attaching a debugger to Visual Studio's designer process. Pretty cool!

I can only learn a couple things from the debugger:

So all I can say that the exception occurs after I've called DataSourceProvider.FinishQuery, and before the designer gets it's hands on the generated DataContext.

Conclusions

I'm stumped. Any ideas?

Update

I created a minimum example to share, and sure enough the exception does show up in the Data pane in expression blend. However, I can actually bind to the generated sample data in the example, so now I think the exception was a red herring and my problem is elsewhere. I'm not even sure if there is an exception, and the Data pane just shows what it does because there COULD be an exception there.

Here's the example if anyone else wants to peek.

Upvotes: 0

Views: 252

Answers (1)

juster zhu
juster zhu

Reputation: 96

Can you show some code? You can look at the problem in a simple way, you can try to bind a single value. If successful, you can prove that your binding part is no problem. Then you can start with your data template.

Upvotes: 1

Related Questions