MVVM - createing在XAML视图与视图模型的DataTemplate当绑定防爆pression错误 [英] MVVM - Binding Expression errors when createing a View in XAML with a ViewModel DataTemplate

查看:100
本文介绍了MVVM - createing在XAML视图与视图模型的DataTemplate当绑定防爆pression错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个WPF应用程序。

背景:

我基本上是有一个向导应用程序。向导是通过提供的ViewModels列表初始化。这些会的ViewModels根据我的XAML一些的DataTemplate创建相应的视图。

在向导中点击下一个或previous,相应的视图模型将被设置和视图将根据该DataTemplate中加载。

这工作得很好。

问题:

当我在一个过渡状态...这意味着正在加载的新视图模型是,似乎有哪里previous观点仍然被引用的一小段时间。正因为如此,我得到了一堆BindingEx pression错误的地方说,它无法找到一组绑定,实际上在preVIOUS视图模型存在。

摘要:我加载基于一个DataTemplate一个新的观点。当这种观点最初加载,这似乎是不同步的实际视图模型。因此我得到了一堆结合前pression错误。

两个问题:


  1. 在如何解决这一问题有什么想法?

  2. 任何危险到有这些BindingEx pression错误?

感谢

 <奇才:WizardContent X:类=WizardContent
             的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
             的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
             的xmlns:WIZ =CLR的命名空间:奇才
             的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
             的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
             的xmlns:模型=CLR的命名空间:Wizard.Models
             的xmlns:视图模型=CLR的命名空间:Wizard.ViewModels
             的xmlns:SYS =CLR的命名空间:系统;装配= mscorlib程序
             的xmlns:鉴于=CLR的命名空间:Wizard.Views
             MC:可忽略=D
             D:DesignHeight =300D:DesignWidth =300>
  <奇才:WizardContent.Resources>
    <! - 用于定义视图此向导的DataTemplates - >
    <数据类型的DataTemplate ={X:类型视图模型:Step1ViewModel}>
      <查看:第一步的DataContext ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType =用户控件},路径= DataContext.CurrentPageVM}/>
    < / DataTemplate中>
    <数据类型的DataTemplate ={X:类型视图模型:Step2ViewModel}>
      <查看:第二步的DataContext ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType =用户控件},路径= DataContext.CurrentPageVM}/>
    < / DataTemplate中>
    <数据类型的DataTemplate ={X:类型视图模型:Step3ViewModel}>
      <观:第三步的DataContext ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType =用户控件},路径= DataContext.CurrentPageVM}/>
    < / DataTemplate中>
  < /行家:WizardContent.Resources>
  < ContentControl中CONTENT ={绑定路径= CurrentPageVM}>
  < / ContentControl中>
< /行家:WizardContent>


解决方案

BindingEx pression错误是没有危险的,据我所知,它只是减慢你的应用程序了一点。

关于你code,我不知道你需要在每个视图定义DataContext的,因为它会自动在DataContext的数据类型进行检测。

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?

Thanks

        <wiz:WizardContent x:Class="WizardContent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:wiz="clr-namespace:Wizards"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:models="clr-namespace:Wizard.Models"
             xmlns:viewModel="clr-namespace:Wizard.ViewModels"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:view="clr-namespace:Wizard.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <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}">
  </ContentControl>
</wiz:WizardContent>

解决方案

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.

这篇关于MVVM - createing在XAML视图与视图模型的DataTemplate当绑定防爆pression错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆