我知道这可能是一个新的复制品,但我没有找到任何解决方案来解决我的问题,或者我脑子里遇到了一些逻辑上的灾难......无论如何,我得到了一个Datagrid,我将Datatable绑定到。现在我想在datagrid / datatable中添加一个新行。这个新行必须插入我的sqlite数据库中。所以我正在侦听RowEditEnding事件以获取那些新值,但看起来我必须首先提交新添加的行 - 所以如何在没有StackOverflowException的情况下提交并获取这些值?
WPF Datagrid
<DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
AutoGenerateColumns="False"
CanUserAddRows="True" CanUserSortColumns="False"
ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemsSource="{Binding Path=MlDb.Tbl_Technik}"
SelectedItem="{Binding TechnicRow, Mode=OneWayToSource}"
IsSynchronizedWithCurrentItem="True" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="InitializingNewItem">
<ic:CallMethodAction MethodName="TeInitializingNewItem" TargetObject="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="RowEditEnding">
<ic:CallMethodAction MethodName="TeRowEditEnding" TargetObject="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="SelectionChanged">
<ic:CallMethodAction MethodName="TeSelectionChanged" TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Technik ID" Width="70" Binding="{Binding Path=TechnikID}"/>
<DataGridTextColumn Header="Techniktyp" Width="100" Binding="{Binding Path=Techniktyp}"/>
<DataGridTextColumn Header="Anlage" Width="80" Binding="{Binding Path=Anlage}"/>
<DataGridTextColumn Header="Anlagengruppe" Width="100" Binding="{Binding Path=Anlagengruppe}"/>
<DataGridTextColumn Header="Bemerkung" Width="110" Binding="{Binding Path=Bemerkung}"/>
</DataGrid.Columns>
</DataGrid>
我的ViewModel的部分内容如下:
public void TeInitializingNewItem(object sender, InitializingNewItemEventArgs e)
{ //Raised if a new row is added
newRowTe = true;
}
public void TeRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{ //
selectedRow = e.Row.GetIndex();
if (newRowTe)
{
// this position would be great to get the values...
}
else
{
rowEditEndTe = true;
DataRowCollection drc = MlDataSetCollection[0].Tbl_Technik.Rows;
Tbl_TechnikRow notUpdatedRow = (Tbl_TechnikRow)drc[selectedRow];
technicId = notUpdatedRow.TechnikID;
}
}
public void TeSelectionChanged(object sender, SelectionChangedEventArgs e)
{ //
if (rowEditEndTe)
{
DataGrid materialDg = (DataGrid)sender;
materialDg.CommitEdit();
DataRowCollection drc = MlDataSetCollection[0].Tbl_Technik.Rows;
Tbl_TechnikRow updatedRow = (Tbl_TechnikRow)drc[selectedRow];
UpdateTblTe(updatedRow);
rowEditEndTe = false;
}
else if (newRowTe)
{
// here I would like to call my function to insert new row in sql db IF rows are changing (if the user changed to another ui element or time is down then I will do the insert command)
}
}
那么还有其他可以听的事吗?或者有人可以告诉我如何获取值?
由于 #endregion