参考我的最后一个问题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>