我有一个绑定到对象列表的数据网格。用户可以在光标所在的位置下方添加一个新行(在代码中,我创建了一个新对象并将其插入到列表中的适当位置)。
假设数据网格有 4 行
如果光标位于第 4 行,则添加该行,但是,如果光标位于任何其他行(1,2 或 3)中,则出现此异常:
System.Windows.Markup.XamlParseException 发生 Message="缺少根元素。" Source="PresentationFramework" LineNumber=0 LinePosition=0 StackTrace: 在 System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException) InnerException: System.Xml.XmlException Message="缺少根元素。” Source="System.Xml" LineNumber=0 LinePosition=0 SourceUri="" StackTrace: 在 System.Xml.XmlTextReaderImpl.Throw(Exception e) 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Windows.Markup.XmlCompatibilityReader。在 System.Windows.Markup.XamlReaderHelper.Read(XamlNode&
注意:当应用程序首次加载时,如果我首先添加一行(通过在最后一行),那么我也可以从任何其他行添加一行。但是,如果我首先尝试从第 1、2、3 行添加一行,那么它会失败!
任何帮助将不胜感激。我完全迷路了。我怀疑其他人是否经历过这种情况,但也许您知道是什么原因造成的,或者我如何调试它,因为我不知道从哪里开始 :(
private void OnAddRowBelowCursor(DataGrid datagrid)
{
try
{
int index = datagrid.SelectedIndex;
MyObject newObj = new MyObject();
ObjectList.Insert(index + 1, newObj);
Logging.log.Info("Appended object row below the cursor...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row below cursor. Reason: " + ex.ToString());
}
}
private void OnAppendRowToBottom()
{
try
{
MyObject newObj = new MyObject();
ObjectList.Add(newObj);
Logging.log.Info("Appended object row to bottom...");
}
catch (Exception ex)
{
Logging.log.Error("Error appending row to the bottom of the table. Reason: " + ex.ToString());
}
}
我还注意到在底部添加一行不会失败
谢谢