E.L*_*unn 1 wpf xaml serialization
希望是一个简单的问题.我有一个自定义控件与依赖项属性,其中包含另一个自定义控件的列表.
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); }
}
Run Code Online (Sandbox Code Playgroud)
然后在xaml中使用它来填充控件
<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>
Run Code Online (Sandbox Code Playgroud)
我现在的问题是我想将其序列化为一个文件但我得到'使用XamlWriter.Save时无法序列化泛型类型'System.Windows.FreezableCollection`'.如果这是一个普通的类,我可以使用属性来描述它应该被序列化的方式(对吗?)但它是一个静态依赖属性,那么如何让它序列化呢?
好傻我在网上有很多关于这个的信息,简单的解决方案是采用通用的freezablecollection并导出一个非泛型类,如下所示.
public class BlockObjectCollection : FreezableCollection<BlockObject>
{
}
Run Code Online (Sandbox Code Playgroud)
然后替换依赖项属性
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); }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1489 次 |
| 最近记录: |