首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在xaml中定义列表

在xaml中定义列表
EN

Stack Overflow用户
提问于 2014-09-24 10:05:13
回答 2查看 291关注 0票数 0

我在做自己的图形控制,它有数字列表

代码语言:javascript
复制
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中定义它有问题:

代码语言:javascript
复制
<local:Graph>
    <local:Graph.Figures>
        <local:Line/> <!-- Property 'Figures' does not support values of type 'Line' -->
    </local:Graph.Figures>
</local:Graph>

如何解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-24 13:20:37

将抽象IList更改为像ListObservableCollection这样的类型应该是解决方案的一部分,因为我不认为xaml引擎只会决定哪种类型应该实现IList。列表的实例化也应该提前发生,我想在xaml中添加一个元素可能只是调用列表上的Add方法。

试试看,如果这对你有效,编译和运行为我。

代码语言:javascript
复制
// 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>());
    }
}
票数 2
EN

Stack Overflow用户

发布于 2014-09-24 10:18:13

来自MSDN中WPF的XAML和自定义类

您的自定义类不能是嵌套类。嵌套类和它们的一般CLR用法语法中的“点”会干扰其他WPF和/或XAML特性,例如附加属性。

因此,在Graph类之外的命名空间中定义类。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26014132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档