Silverlight DataGrid CellEditTemplate组合框数据绑定问题。 [英] Silverlight DataGrid CellEditTemplate combobox databinding issue.
问题描述
我正在使用Entity Framework后端的RIA服务在ASPX上开发SilverLight应用程序。我有很好的加载DataGrid包括相关的表数据。我现在正在使用组合框更改来更新字段。在我调试时,我可以看到实体对象的更改,但它不会持久存在。我尝试在RowEditEnding事件中调用DataGrid上的CommitEdit,它只是让应用程序崩溃而导致错误。
绑定CB:
I am working on a SilverLight app on ASPX with RIA services from Entity Framework backend. I have the loading of the DataGrid just fine including the related table data. I am now working on updating the fields with the combobox changes. As I debug I can see the changes on the entity object, but it does not persist. I tried calling CommitEdit on the DataGrid in the RowEditEnding event which just crashes the app with giving me the error.
Bind the CB's:
Private Sub dgStaff_PreparingCellForEdit(sender As Object, e As System.Windows.Controls.DataGridPreparingCellForEditEventArgs) Handles dgStaff.PreparingCellForEdit
columnIndex = e.Column.DisplayIndex
Dim cb As ComboBox = TryCast(e.EditingElement, ComboBox)
If cb IsNot Nothing Then
cb.DisplayMemberPath = "Description"
Select Case columnIndex
Case 2
cb.SelectedItem = curPerson.Station.StationId
cb.ItemsSource = stations
cb.SelectedValue = curPerson.Station.StationId
Case 3
cb.SelectedItem = curPerson.Shift.ShiftId
cb.ItemsSource = shifts
cb.SelectedValue = curPerson.Shift.ShiftId
Case 4
cb.SelectedItem = curPerson.Team.TeamId
cb.ItemsSource = teams
cb.SelectedValue = curPerson.Team.TeamId
End Select
End If
End Sub
获取当前实体对象:
Get the current entity object:
Private Sub dgStaff_BeginningEdit(sender As Object, e As System.Windows.Controls.DataGridBeginningEditEventArgs) Handles dgStaff.BeginningEdit
curPerson = CType(e.Row.DataContext, BMServiceRef.Personnel)
End Sub
获取新值并将其应用于实体对象:
Get the new values and apply them to the entity object:
Private Sub cb_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
Dim cb As ComboBox = DirectCast(sender, ComboBox)
Select Case columnIndex
Case 2
curPerson.StationId = CType(cb.SelectedItem, BMServiceRef.Station).StationId
Case 3
curPerson.ShiftId = CType(cb.SelectedItem, BMServiceRef.Shift).ShiftId
Case 4
curPerson.TeamId = CType(cb.SelectedItem, BMServiceRef.Team).TeamId
End Select
End Sub
如果我可以添加更多信息,请告诉我。谢谢你看...
Let me know if I can add any more info. Thanks for looking...
推荐答案
我有一个解决方法。在combobox selectionChanged事件中,我将选择保存到服务器。在cellEditEnd事件中,我再次更新dataGrid。我仍然希望得到组合框中显示的当前值 - selectedIndex。如果有人可以帮助那部分(或其他部分),我会给你解决方案。
这两个绑定不起作用,这就是我必须重新绑定dataGrid的原因。
I have a workaround. In the combobox selectionChanged event I save selection to server. In the cellEditEnd event I update the dataGrid again. I would still like to get the current value to show in the combobox - the selectedIndex. If someone can help with that part(or the rest) I'll give you the solution.
The twoWay binding does not work and this is why I have to rebind the dataGrid.
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=stations,Mode=TwoWay}" SelectionChanged="cb_SelectionChanged" DropDownClosed="dd_closed" />
</DataTemplate>
你需要绑定SelectedValue
< combobox height =23>
DisplayMemberPath =name
SelectedValuePath =id
SelectedValue ={Binding Path = buildingId,Mode = TwoWay}
ItemsSource ={Binding Buildings}
宽度=160>
You need to bind the SelectedValue
<combobox height="23">
DisplayMemberPath="name"
SelectedValuePath="id"
SelectedValue="{Binding Path=buildingId, Mode=TwoWay}"
ItemsSource="{Binding Buildings}"
Width="160">
这篇关于Silverlight DataGrid CellEditTemplate组合框数据绑定问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!