I have a custom WPF class subclassing RickTextBox so I can add inline displays for custom data objects. Occasionally, I need to clear everything and rebuild the document from the list of classes. When I do that, though, I get a strange error:
doc.Blocks.Clear(); // <-- Error at this line
--------------------------------------------------------------
Additional information: Cannot serialize a generic type System.Collections.ObjectModel.ObservableCollection [System.Collections.ObjectModel.ObservableCollection ...
I really have no clue what could be causing this. I'm not doing any explicit serialization so something within WPF is trying to. Does anybody know what's going on?
FYI, here is the custom inline display for my object (don't worry, the code-behind work will eventually migrate to XAML). I've narrowed the problem down to CellVMs
, which is an IEnumerable<IEnumerable<ReportTableCellViewModel>>
public class ReportTableRun : InlineUIContainer
{
public ReportTableRun() : this(null)
{
}
public ReportTableRun(ReportTableViewModel table, DataTemplate cellTemplate=null) : base()
{
mTable = table;
DynamicGrid grid = new DynamicGrid();
grid.DefaultRowHeight = new GridLength(50);
grid.DefaultColumnWidth = new GridLength(50);
Binding bind0 = new Binding("CellVMs");
bind0.Mode = BindingMode.OneWay;
bind0.Source = mTable;
BindingOperations.SetBinding(grid, DynamicGrid.ItemsSourceProperty, bind0);
Binding bind3 = new Binding("RowSharedGroupNames");
bind3.Mode = BindingMode.OneWay;
bind3.Source = mTable;
BindingOperations.SetBinding(grid, DynamicGrid.RowSharedSizeGroupNamesProperty, bind3);
Binding bind4 = new Binding("ColumnSharedGroupNames");
bind4.Mode = BindingMode.OneWay;
bind4.Source = mTable;
BindingOperations.SetBinding(grid, DynamicGrid.ColumnSharedSizeGroupNamesProperty, bind4);
grid.ItemTemplate = cellTemplate;
this.Child = grid;
}
private ReportTableViewModel mTable;
}
and here is the code for rebuilding the document.
private void BuildDocument()
{
FlowDocument doc = Document;
IEnumerable<IReportComponentViewModel> components = ReportTemplateViewModel.ComponentVMs;
List<Paragraph> parList = new List<Paragraph>();
Paragraph par = new Paragraph();
parList.Add(par);
foreach(IReportComponentViewModel c in components)
{
if (c is ReportTableViewModel)
par.Inlines.Add(new ReportTableRun(c as ReportTableViewModel, ReportTableCellTemplate));
}
doc.Blocks.Clear();
foreach(Paragraph p in parList)
doc.Blocks.Add(p);
}