Reputation: 551
Hopefully a simple question. I have a custom control with a dependency property that contains a list of another custom control.
public static readonly DependencyProperty BlockObjectsProperty = DependencyProperty.Register("BlockObjects", typeof(FreezableCollection<BlockObject>), typeof(Block), new FrameworkPropertyMetadata(new FreezableCollection<BlockObject>(), null));
public FreezableCollection<BlockObject> BlockObjects
{
get { return (FreezableCollection<BlockObject>)base.GetValue(BlockObjectsProperty); }
set { base.SetValue(BlockObjectsProperty, value); }
}
this is then used within xaml to populate the controls
<Viewbox Grid.Row="2" Stretch="Uniform">
<ItemsControl x:Name="tStack" ItemsSource="{TemplateBinding BlockObjects}" ContextMenu="{StaticResource BodyContextMenuKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Viewbox>
My problem now is i want to serialize this out to a file but i'm getting 'Cannot serialize a generic type 'System.Windows.FreezableCollection`' when using XamlWriter.Save. if this was a normal class i could use attributes to describe the way it should be serialized (right?) but its a static dependency property so how do i get this to serialize?
Upvotes: 3
Views: 1959
Reputation: 551
Ok Silly me there is alot of information about this all over the net, the simple solution is to take the generic freezablecollection and derive a none generic class as below.
public class BlockObjectCollection : FreezableCollection<BlockObject>
{
}
then replace the dependency properties
public static readonly DependencyProperty BlockObjectsProperty = DependencyProperty.Register("BlockObjects", typeof(BlockObjectCollection), typeof(Block), new FrameworkPropertyMetadata(new BlockObjectCollection(), null));
public BlockObjectCollection BlockObjects
{
get { return (BlockObjectCollection)base.GetValue(BlockObjectsProperty); }
set { base.SetValue(BlockObjectsProperty, value); }
}
Upvotes: 8