我有一个可以通过PropertyGrid编辑的业务对象。它包含标签列表(比如说用于简化的字符串)。
public class MyObject
{
// bunch of properties here, cut for readiness
public LabelsList List {get;set;}
// ...
}标签列表是从List继承的简单类:
public class LabelsList : List<T>
{}为了在属性网格中正确显示我的对象(我也是指可扩展的标签可编辑列表),我为LabelsList实现了一个LabelsList,特别是更改了GetProperties()方法:
public PropertyDescriptorCollection GetProperties()
{
var props = new PropertyDescriptorCollection(null);
for (var i = 0; i < this.Count; i++)
{
var descriptor = new LabelsListPropertyDescriptor(this, i);
props.Add(descriptor);
}
return props;
}现在的问题是,当我通过在底层类型上调用XamlWriter.Save(this)来使用标准的XAML序列化时,它在产生的XAML中添加了过多的LabelsList.LabelName标记:
<wpfdl:LabelsList>
*<wpfdl:LabelsList.Label1Name>*
<wpfdl:Label LabelText="Label1Name"/>
*</wpfdl:LabelsList.Label1Name>*
...
</wpfdl:LabelsList>这实际上禁用了后续(MyObject)XamlReader.Parse(exportedXaml)调用,因为标签名称可以包含特殊字符。实现对象的正确编辑和序列化的正确解决方法是什么?提前谢谢。
更新
通过更改相应的PropertyDescriptor使不必要的标记消失:
public override bool ShouldSerializeValue(object component)
{
return false;
}由此产生的xaml如下(原语是我的对象自定义类型的名称):
<Primitive>
<Primitive.Labels>
<Label LabelText="text1" LabelPosition="position1" />
<Label LabelText="text2" LabelPosition="position2" />
</Primitive.Labels>
</Primitive>差不多就是这样,但是现在中的标记已经丢失了:
'Collection property 'WPF_DrawingsTest.Primitive'.'Labels' is null.' Line number '1' and line position '96'.还得把这事做好。也许测试项目会帮助我了解我在寻找什么:
测试项目,100 kb,无病毒
发布于 2011-12-16 08:44:26
重构初始业务对象,序列化/反序列化现在自动完成,工作正常。
https://stackoverflow.com/questions/7552440
复制相似问题