First of all, some context. If you are familiar with the problem, go to the BindingExpression part. This is my first major project in WPF, so I'm still new to the MVVM template. Here is the only other similar question I have found whose dull answer really doesn't really interest me.
I / I am creating a .NET 3.5 WPF application and I am using MVVM (implemented by myself, without a framework). In this case, I have a number of Views and ViewModels . They are within the leading ApplicationView and ApplicationViewModel respectively.
A way to change the views is to use the XAML DataTemplate elements in the ApplicationView , for example:
<DataTemplate DataType="{x:Type viewmodels:InitViewModel}"> <views:InitView /> </DataTemplate>
And then in the main part, I have a ContentControl that binds to a property in ApplicationViewModel
<ContentControl Content="{Binding CurrentPageViewModel}"/>
When I run the application, it all seems to work fine and does exactly what it is intended to do. However, when I look at the output of Debug after startup, I get a lot of BindingExpression errors.
Here is one example. I have a SplashText property in my InitViewModel . This is due to the text block in the splash screen ( InitView ). When the splash screen ends and I exit the view mode, I get the following:
System.Windows.Data Error: 39 : BindingExpression path error: 'SplashText' property not found on 'object' ''MainMenuViewModel' (HashCode=680171)'. BindingExpression:Path=SplashText; DataItem='MainMenuViewModel' (HashCode=680171); target element is 'TextBox' (Name='FeedBackBox'); target property is 'Text' (type 'String')
I understand that this is because the bindings still exist, but the CurrentPageViewModel DataContext property has changed. So I want to know the following:
- Is this a fleeting problem, i.e. Are representations that are not used, or are they (and bad bindings) in memory there endlessly?
- Is there a way to clear or deactivate these bindings while the view is inactive?
- What kind of performance knock will have in my application if I leave them alone?
- Is there a better way to switch views that avoids this problem?
Thanks in advance and apologize for the monolithic question.
Edit 03/09/13 - Thanks to Jehof, Francesco De Lisi and Faster Solutions, noting that it is pointless to set the sub-views datacontext as {Binding DataContext.CurrentPageViewModel, RelativeSource={RelativeSource AncestorType={x:Type Window}}} , because ContentControl takes care of the datacontext.