VS Design Time DataContext (Sample Data) Contains Exception - c#

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.
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:
My viewmodel being generated by my DataSourceProvider is valid, looks good, no exceptions thrown.
My DataSourceProvider's Error field is null when it leaves my code.
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.

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.

Related

Binding using Source and x:Reference leads to design-time error

for our WPF application I have designed a CustomControl to mimic some flyout-like behavior. The flyout basically just consists of a FrameworkElement which holds the content that is placed inside the flyout.
Controls that are placed in the FrameworkElement of the flyout are, of course, bound to other controls. However, I am confused as this results in several problems:
I can not use ElementName=sourceName to set the source of the binding. Instead I have to use Source={x:Reference sourceName} to denote the source.
The XAML editor of VisualStudio marks all lines where a binding of this type occurs and the ErrorList shows "Object reference not set to an instance of an object" for each occurrence of the binding (no exception is thrown within the designer view).
The project builds successfully and during runtime all bindings work flawlessly! Besides that, it does not matter if the flyout is implemented as a UserControl or a CustomControl.
If a control that is not placed in the flyout is bound to the same source I can use ElementName=sourceName without any problems and the correct values are displayed in the designer.
As proposed in some other replies concerning this error I tried to attach the VS instance to the debugger of a second instance of VS but no errors/exceptions are displayed there.
As already mentioned, everything works at runtime. However, as multiple developers are working on this project it is not acceptable to have any errors at all; warnings would be acceptable to a certain extend.
We know that using a binding proxy (like it is proposed in e.g. https://stackoverflow.com/a/22531679/119177) should/could solve the problem. However, because the current binding is actually working, we hope to solve the design-time problem some other way (maybe even with just suppressing the error message).
Thanks for your help!

User control accumulates dependency property bindings

I have a user control which is a labelled combo box, through debugging I noticed that each time I open the view which contains the user control there is one more get request to each of the models properties than the previous time!
I do re-instantiate the model each time the view is opened (for reasons that don't really need mentioning), but what appears to be happening is that the binding to any previous copies of the model are retained and the model seems to not be being disposed of correctly because it is still tied into the program through this binding! I appreciate that multiple bindings to the dependency property can be sustained, but how do I stop this happening?
Two considerations I have thought of:
1) The usercontrol is not being disposed of when the view is closed, if this is possible should I turn my attention towards ensuring it is being disposed of? Or is this overkill to the needs of WPF?
2) Instead of re-instantiating the model each time the view is opened should I just implement a copy function in the model base class which transfer all of the properties over opposed to actually creating another instance of the model?
What do you think?
Thanks
Finally fixed this problem, the issue was not limited to my user controls but in fact all controls (including the standard WPF ones) with bindings! When the window was being closed the bindings were not being terminated, this bug/issue/design (maybe it has a use - although I can't see what that would be) seems very strange to me. Initially I did think maybe it was the way I set the bindings up, however with one of the bindings to a normal text box I simply use Text="{Binding name}" - no other configuration defined which could be causing this issue!
Anyway, the solution I found was to add BindingOpertations.ClearAllBindings(this) to the windows closing event. Hope this helps if you are having this problem.

Window content change: Specified element is already logical child

So I realize this is an often-asked about error, and I've read many results from google, but I still can't figure out how it applies to my situation.
The setup: I create a window to present views. I create an instance of the first view and set the window's Content property to the view. Currently each of my views is a window itself. This is working perfectly on 7/10 computers. However, for some reason on a few computers (verified correct .NET version), switching to the second view (creating an instance of the second view and setting the window's Content property to the second view) throws a System.InvalidOperationException Specified element is already the logical child of another element. Disconnect it first.
I read that using ContentTemplate instead of Content would solve the problem, but that just produces a blank screen instead of the second view (I'm sure I'm doing that wrong).
I've also read that you should use a UserControl instead of a Window as the Window's Content property, so I'm trying that (I don't have a computer that's getting this error, so I have to wait to hear back about success). But it seems to me that that won't really change anything.
Any ideas why this is only happening on some computers, and any ideas on how to fix it?
Edit: Changing the views to UserControls did not solve the issue
Edit: I realized that I misspoke. I don't set the Content of the main window to a Window, I set it to the second Window's Content. So like:
MainWindow.Content = nextScreen.Content;
Thanks for the answers everyone. I discovered that changing my views to UserControls was the correct path. When I tried it the first time I made this mistake:
window.Content = userControl.Content;
instead of
window.Content = userControl;
which still worked on my machine, but continued to fail on the computers that were previously having problems.
I still don't understand why only a handful of computers were throwing an exception over this. Obviously there is some difference between them, though it wasn't anything obvious like Windows version or processor architecture.

How to change control template from the view model in WP7?

Hi basically I've been able to completely seperate my logic into a view model with the exception of one thing. I have a a button and two different templates that can be applied to the button. Basically when audio is playing one template is displayed and when its not the other is displayed. When I was using the code behind this was easy because I had access to the page's Resources and all I had to do was set it accordingly. Now that my code is in the VM though I'm not really sure how to change the template.
The first thought that comes to mind is to maybe send a message to the view to change it when I need to. Is this the only way? I did see that WPF had triggers which looks pretty nice because you don't have to send messages and no code is placed in the code behind. Has anyone found a work around for this?
You can probably write a ValueConverter that would look at a public bool IsPlaying property and transform it to the appropriate template. Then you can databind your Button template property to the IsPlaying property, using the converter as a translator.
I haven't tried this with templates, but I've used it to great success with Bitmap and Visibility, so I'm pretty sure it would work here as well.
Here's a good intro to ValueConverter, if you haven't used one before.
That said, messaging isn't such a horrible solution in this case. Sometimes there is code that just wants to live in the codebehind file. Animation start/stop code is another example that comes to mind.

WPF - Have a list source defined at runtime but still have sample data for design time

I have some ListBoxes in my WPF app. I would like to be able to view how the design looks with out having to run the app.
But I still want to be able to bind to ItemsSource to my View Model.
I know I saw a blog post on how to do this, but I cannot seem to find it now.
To reiterate, I want dummy data at design time, but real data at run time and not break the MVVM pattern.
Any ideas?
You can check whether your code is in design mode or not. Here's a great post about doing this in different situation.
Detecting design time mode in WPF and Silverlight
Have you tried the sample data option in Blend 3? With a control open, look at the DATA tool window. There's a way to create sample data... think it does exactly what you're looking for:
http://silverzine.com/tutorials/how-to-create-sample-data-in-blend-3/

Categories