将Caliburn Micro绑定到EF实体错误

时间:2016-04-18 11:59:12

标签: c# wpf mvvm caliburn.micro

我将实体框架6.0实体绑定到Caliburn Micro View:

<aura:AuditView Grid.Row="0" x:Name="SelectedAudit" cal:View.Model="{Binding SelectedAudit}" cal:View.Context="SelectedAudit"/>

屏幕上出现的错误是:

"Cannot find view for System.Data.Entity.DynamicProcies.Audit_9B5A..."

SelectedAudit是ViewModel上的实体属性。我应该创建从+到实体到新AuditModel的地图(AutoMapper)吗?还是我错过了一些神奇的东西?

编辑 aura:AuditView

的代码
<UserControl x:Class="Aura.AuditView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:aura="clr-namespace:Aura"
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="LockedTextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="Background" Value="#FFEFE2E2"/>
        </Style>
    </UserControl.Resources>

    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="Description" HorizontalAlignment="Right"/>
        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Description}" Margin="3" />

        <Label Grid.Row="1" Grid.Column="0" Content="User" HorizontalAlignment="Right"/>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding UserId}" Margin="3" Style="{StaticResource LockedTextBox}"/>

        <Label Grid.Row="0" Grid.Column="2" Content="Begin date" HorizontalAlignment="Right"/>
        <DatePicker Grid.Row="0" Grid.Column="3" SelectedDate="{Binding BeginDate}" Margin="3"/>

        <Label Grid.Row="1" Grid.Column="2" Content="Deadline" HorizontalAlignment="Right"/>
        <DatePicker Grid.Row="1" Grid.Column="3" SelectedDate="{Binding Deadline}" Margin="3"/>

        <Label Grid.Row="2" Grid.Column="2" Content="End date" HorizontalAlignment="Right"/>
        <TextBox Grid.Row="2" Grid.Column="3" Text="{Binding EndDate, StringFormat=dd/MM/yyyy}" Margin="3" Style="{StaticResource LockedTextBox}"/>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

我相信你真的想要

cal:Bind.Model="{Binding SelectedAudit}"

否则,您正在尝试执行viewmodel-first resolution,在这种情况下,Caliburn Micro将寻求解析VM的视图,而不是使用您提供的视图。

e.g。

<aura:AuditView Grid.Row="0" x:Name="SelectedAudit" cal:Bind.Model="{Binding SelectedAudit}" />