-1

参考我的最后一个问题https://stackoverflow.com/questions/18568850/binding-in-combobox-xaml-wpf,我在 ClientBookKeepingViewModel 中添加了一个属性 FirstName。但现在我得到了一个错误在此处输入图像描述

当我使用断点调试时,错误来自 View.ShowDialog()在此处输入图像描述

我的代码如下:

  public int InsertClientBooking(int clientID, ClientBookKeepingViewModel k)
    {
        var client = new ClientBooking();

        using (var context = new ProActiveDBEntities())
        {
            client.ClientBookID = k.ClientBookKeepingID;
            client.ClientID = k.ClientID;
            client.EmployeeID = k.EmployeeID;
            client.WorkType = k.WorkType;
            client.DateRecorded = (DateTime)k.DateRecorded;
            client.BookingFormCompleted = k.BookingFormCompleted;
            client.TimeBudgetCompleted = k.TimeBudgetCompleted;
            client.ProposedCompletionDate = (DateTime)k.ProposedCompletionDate;
            client.IsCompleted = k.IsCompleted;
            client.FirstName = k.FirstName;

            context.Clients.First(i => i.ClientID == clientID).ClientBookings.Add(client);

            context.SaveChanges();

            return clientID;
        }
    }



      private void btnAddBooking_Click(object sender, RoutedEventArgs e)
    {
        ClientBookingView view = new ClientBookingView();
        ClientBookKeepingViewModel book = new ClientBookKeepingViewModel();

        book.Client = (ClientViewModel)this.DataContext;

        book.Mode = Mode.Add;
        view.DataContext = book;
        view.ShowDialog();
    }

客户端BookingView.Xmal

<ComboBox Grid.Column="1" Grid.Row="1" Name="cbEmployeeName" 
 ItemsSource="{Binding Source={StaticResource Employee}}" 
 DisplayMemberPath="FirstName" 
 SelectedValue="{Binding Path=FirstName, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
 SelectedValuePath="FirstName"

<CheckBox Grid.Column="1" Grid.Row="5" Name="dpBookingCompleted" Content="Yes" 
 IsChecked="{Binding BookingFormCompleted, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
/>
<StackPanel Grid.Row="9"  Orientation="Horizontal" Grid.ColumnSpan="3" Margin="0,5,20,0" Grid.Column="1">
        <Button x:Name="btnUpdate" Width="80" Margin="10" Height="25"
                    Command="{Binding ShowUpdateCommand}" 
                    Click="btnUpdate_Click">

            <Button.Style>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MetroButton}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Mode}">
                            <DataTrigger.Value>
                                <enum:Mode>Add</enum:Mode>
                            </DataTrigger.Value>
                            <Setter Property="Content" Value="Add"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Mode}">
                            <DataTrigger.Value>
                                <enum:Mode>Edit</enum:Mode>
                            </DataTrigger.Value>
                            <Setter Property="Content" Value="Save"/>
                        </DataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=cbEmployeeName, Path=(Validation.HasError)}" Value="false"/>
                                <Condition Binding="{Binding ElementName=cbWorkType, Path=(Validation.HasError)}" Value="false"/>
                                <Condition Binding="{Binding ElementName=dpDateRecord, Path=(Validation.HasError)}" Value="false"/>
                                <Condition Binding="{Binding ElementName=dpProposedCompletionDate, Path=(Validation.HasError)}" Value="false"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="IsEnabled" Value="True"/>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

        <Button x:Name="btnCancel" Content="Cancel" Margin="10" Width="80" Height="25"
                    Command="{Binding CancelCommand}"
                    Click="btnCancel_Click" />
    </StackPanel>

客户端BookListView

 <ListView Name="lsvClientOwnerTypeList" Height="150" Width="700"
                  ScrollViewer.VerticalScrollBarVisibility="Visible" 
                  ScrollViewer.HorizontalScrollBarVisibility="Visible" 
                  HorizontalAlignment="Center"
                  VerticalAlignment="Top"
                  IsSynchronizedWithCurrentItem="True"
                  ItemsSource="{Binding ClientBookList}"
                  SelectedItem="{Binding SelectedClientBook}">

            <ListView.View>
                <GridView>
                    <GridViewColumn CellTemplate="{StaticResource RowButtons}"/>
                    <GridViewColumn Header="Completed" DisplayMemberBinding="{Binding IsCompleted}"  />
                    <GridViewColumn Header="Employee Name" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="Work Type" DisplayMemberBinding="{Binding WorkType}"  />
                    <GridViewColumn Header="Date Recorded" DisplayMemberBinding="{Binding DateRecorded, StringFormat={}\{0:dd/MM/yyyy\}}"  />
                    <GridViewColumn Header="Booking Form Completed" DisplayMemberBinding="{Binding BookingFormCompleted}"  />
                    <GridViewColumn Header="Time Budget Completed" DisplayMemberBinding="{Binding TimeBudgetCompleted}"  />
                    <GridViewColumn Header="Proposed Completion Date" DisplayMemberBinding="{Binding ProposedCompletionDate, StringFormat={}\{0:dd/MM/yyyy\}}"  />
                </GridView>
            </ListView.View>
        </ListView>
4

1 回答 1

0

我正在回答一个答案,以便评论不会变得太大。

你内心的异常说:

"INSERT 语句与 FOREIGN KEY 约束 \"FK_ClientBooking_Employee\" 冲突。冲突发生在数据库 \"ProActiveDB\"、表 \"dbo.Employee\"、列 'EmployeeID' 中。\r\n语句已终止。 "

这意味着Foreign Key您的Employee表中有一个EmployeeID名为的列FK_ClientBooking_Employee,并且当您尝试将某些内容插入数据库时​​发生冲突......如果冲突与此列有关,那么这通常意味着您试图插入一个行到另一个表中,该表中引用了该列Employee,但您的EmployeeID不在Employee表中。

更新>>>

我不确定您是否真的需要将该列添加到数据库中……这就是视图模型的用途。您的类中有FirstNameEmployee(在 C# 中),所以您需要做的就是在视图模型类中添加一个新属性并从您的Employee类中填充它......然后,您将能够正确绑定到它。

请记住,视图模型应该包含我们视图所需的所有数据……但这并不意味着我们的视图模型必须匹配某些数据库表。实际上,视图模型和数据库表之间通常几乎没有关联……它是与数据库表匹配的数据类型或模型类。

于 2013-09-02T14:47:38.283 回答