0

我有一个绑定到对象列表的数据网格。用户可以在光标所在的位置下方添加一个新行(在代码中,我创建了一个新对象并将其插入到列表中的适当位置)。

假设数据网格有 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());
        }
    }

我还注意到在底部添加一行不会失败

谢谢

4

2 回答 2

0

看起来您正在加载的数据格式错误。我可能会向您推荐这样的解决方案,即使验证失败,您也可以解析任何 XML 或 HTML 源

于 2011-03-11T13:59:13.680 回答
0

我正在使用带有 XAMLFormatter 的扩展库中的 RTB。

创建新行时,我没有将空字符串转换为 XAML 格式。为什么它只在我将它添加到光标下方而不是最后时失败,我仍然不知道。但它是固定的

于 2011-03-18T08:25:39.220 回答