MVVM - Binding Expression errors when createing a View in XAML with a ViewModel DataTemplate
Asked Answered
C

1

1

This is a WPF application.

Background:

I essentially have a Wizard application. The Wizard is initialized by providing a list of ViewModels. These ViewModels will create the appropriate view based on some DataTemplate in my XAML.

When clicking next or previous in the Wizard, the appropriate ViewModel will be set and the view will be loaded based on the DataTemplate.

This works fine.

Problem:

When I'm in a transitory state...meaning the new viewModel is being loaded, it appears there is a brief amount of time where the previous VIEW is still being referenced. Because of this, I get a bunch of BindingExpression errors where it says it cannot find a bunch of bindings which actually existed on the PREVIOUS viewModel.

SUMMARY: I'm loading a new view based on a DataTemplate. When that view is initially loaded, it appears to be out of sync with the actual viewModel. As such I get a bunch of binding expression errors.

Two questions:

  1. Any thoughts on how to fix this?
  2. Any dangers to having these BindingExpression errors?

    <wiz:WizardContent.Resources>
        <!--DataTemplates for defining views for this Wizard-->
        <DataTemplate DataType="{x:Type viewModel:Step1ViewModel}">
            <view:Step1 DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=DataContext.CurrentPageVM}"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type viewModel:Step2ViewModel}">
            <view:Step2 DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=DataContext.CurrentPageVM}"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type viewModel:Step3ViewModel}">
            <view:Step3 DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=DataContext.CurrentPageVM}"/>
        </DataTemplate>
    </wiz:WizardContent.Resources>
    <ContentControl Content="{Binding Path=CurrentPageVM}"/>
    

Ceiling answered 25/4, 2013 at 17:25 Comment(2)
need to see the viewmodel code, are you implementing inotifypropertychanged on the viewmodel properties?Purdah
thanks will add that tonight when I get homeCeiling
K
0

BindingExpression errors aren't dangerous, as far as I know of, it just slows your app a little.

Concerning your code, I'm not sure you need to define DataContext in each view, since it automatically takes the DataContext detected by the DataType.

Kehr answered 26/4, 2013 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.