我在做自己的图形控制,它有数字列表
public class Figure
{
public virtual void Render(Graph graph, GDI.Graphics graphics) { }
}
// was nested before, that's why here is @jnovo answer
public class Line : Figure { ... }
public class Plot : Figure { ... }
... // more figures
[ContentProperty("Figures")] // this doesn't help
public class Graph : FrameworkElement
{
public IList<Figure> Figures { get; set; }
}我在xaml中定义它有问题:
<local:Graph>
<local:Graph.Figures>
<local:Line/> <!-- Property 'Figures' does not support values of type 'Line' -->
</local:Graph.Figures>
</local:Graph>如何解决这个问题?
发布于 2014-09-24 13:20:37
将抽象IList更改为像List或ObservableCollection这样的类型应该是解决方案的一部分,因为我不认为xaml引擎只会决定哪种类型应该实现IList。列表的实例化也应该提前发生,我想在xaml中添加一个元素可能只是调用列表上的Add方法。
试试看,如果这对你有效,编译和运行为我。
// without DependencyProperty
public class Graph : FrameworkElement
{
// Figures
public List<Figure> Figures { get; set; }
public Graph()
{
Figures = new List<Figure>();
}
}
// with DependencyProperty
public class Graph : FrameworkElement
{
// Figures
private static readonly DependencyPropertyKey FiguresPropertyKey = DependencyProperty.RegisterReadOnly("Figures", typeof(ObservableCollection<Figure>), typeof(Graph), new FrameworkPropertyMetadata(new ObservableCollection<Figure>()));
public static readonly DependencyProperty FiguresProperty = FiguresPropertyKey.DependencyProperty;
public ObservableCollection<Figure> Figures { get { return (ObservableCollection<Figure>)GetValue(FiguresProperty); } }
public Graph()
{
// explanation for this, see http://msdn.microsoft.com/en-us/library/aa970563%28v=vs.110%29.aspx
SetValue(FiguresPropertyKey, new ObservableCollection<Figure>());
}
}发布于 2014-09-24 10:18:13
来自MSDN中WPF的XAML和自定义类
您的自定义类不能是嵌套类。嵌套类和它们的一般CLR用法语法中的“点”会干扰其他WPF和/或XAML特性,例如附加属性。
因此,在Graph类之外的命名空间中定义类。
https://stackoverflow.com/questions/26014132
复制相似问题