对象绑定与面向对象的继承 [英] Object Binding Vs Object Oriented Inheritance

查看:89
本文介绍了对象绑定与面向对象的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我对使用绑定到例如 ListBox 使用继承的对象有疑问.

I have a question about using binding to for example to ListBox, objects which are using Inheritance.

最好通过示例进行解释:

Best to explain by example:

    public class Session
    {
        public DateTime dateCreated { get; set; }
        public String name { get; set; }
        public String imageUrl { get; set; }
        public int time { get; set; }

    }


{
    class Distance : Session
    {
        Double distance;
     
        public Distance(String name, String imageUrl, int time, Double distance)
        {
            this.dateCreated = DateTime.Now;
            this.name = name;
            this.imageUrl = imageUrl;
            this.time = time;
            this.distance = distance;
        }
    }


    class Weight : Session
    {
        int numberOfSets;
        int numberOfReps;
        double weight;

        public Weight(String name, String imageUrl, int time, int numberOfSets, int numberOfReps, double weight)
        {
            this.dateCreated = DateTime.Now;
            this.name = name;
            this.imageUrl = imageUrl;
            this.time = time;
            this.numberOfSets = numberOfSets;
            this.numberOfReps = numberOfReps;
        }

    }

这里我有一个叫 Session 的类,其中 Weight Distance 继承.显然 Weight Distance 具有不同的参数,例如:Weight具有 numberOfSet s,距离没有它.

Here I have class called Session which Weight and Distance inherits from. Obviously Weight and Distance have different parameters like: Weight has numberOfSets where Distance doesn't have it.

现在我想要实现的是将 Session 列表绑定到 ListBox ,但是以某种方式处理了会话可以具有两种类型的事实: 体重距离.理想情况下,我想在其中包含两个 Item Templates ListBox ,所以我可以显示然后包括额外信息.

Now what I want to achieve is to bing a List of Session to a ListBox but somehow deal with the fact that session can have two types: Weight and Distance. Ideally I would like to have two Item Templates inside of ListBox so I can display then including the extra information.

希望你们能理解我来自哪里.预先非常感谢.

I hope you guys understand where I am coming from. Many thanks in advance.

推荐答案

The Fouller,

Hi TheFouller,

您需要一个DataTemplateSelector,它使您可以在运行时根据绑定控件的详细信息选择其他DataTemplate.

What you need is a DataTemplateSelector which will let you choose a different DataTemplate at runtime based on the details of the bound control.

困难在于Windows Phone没有内置的DataTemplateSelector(WPF和Windows Store应用程序具有).但是,有很多帖子可以演示如何实现.您可以找到几个链接 此搜索之后.我实际上还没有尝试过它们,但是我检查过的看起来像它们应该工作,并且应该很容易实现.

The hitch is that Windows Phone doesn't have a built in DataTemplateSelector (WPF and Windows Store apps do); however, there are a number of posts where people demonstrate how to implement one. You can find several links behind this search. I haven't actually tried any of them, but the ones I checked look like they should work and should be fairly straightforward to implement.

-Rob


这篇关于对象绑定与面向对象的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆