首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >msdn数据模板概述

msdn数据模板概述
EN

Stack Overflow用户
提问于 2010-12-15 22:51:11
回答 1查看 230关注 0票数 1

我正在关注msdn数据模板概述http://msdn.microsoft.com/en-us/library/ms742521.aspx

但我觉得他们错过了对他们拥有的something..for资源的解释:

代码语言:javascript
复制
 <Window.Resources><local:Tasks x:Key="myTodoList"/></Window.Resources>

而他们在XAML中所拥有的是

代码语言:javascript
复制
<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"/>

在不显示C#代码的情况下,它们能够在ListBox中显示项目列表。ListBox没有x:Name,并且我不能在MainWindow中添加项,并且使用单独的类Tasks,我执行了以下操作(这不起作用)

代码语言:javascript
复制
using System.Collections; // use ArrayList
using System.Collections.ObjectModel; // use Observable Collection

namespace FooApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class Tasks : ObservableCollection<Tasks>
    {
        string TaskName;
        string Description;
        int Priority;

        public TasksStuff(string taskName, string description, int priority)
        {
            this.taskName = TaskName;
            this.description = Description;
            this.priority = Priority;
        }

        public string TaskName
        {get{return this.taskName}}

        public string Description
        {get{return this.description}}

        public string Priority
        {get{return this.priority}}

        private ArrayList Tasks
        {
            ArrayList taskList = new ArrayList();
            taskList.Add( new TasksStuff("A foo task", "doing foo",1));
            return taskList;
        }
    }
}

我真的很困惑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-15 23:06:39

这应该是Tasks的定义,您的示例才能正常工作:

代码语言:javascript
复制
public class Tasks : ObservableCollection<Task /*or whatever type you want to use here*/>
{
    //...
}

-编辑--

代码语言:javascript
复制
// This is a class to store information for a single task
// It has nothing to do with a collection of tasks
public class Task
{
    private String _taskName;
    public String TaskName
    {
        get { return _taskName; }
        set { _taskName = value; }
    }

    private String _description;
    public String Description
    {
        get { return _description; }
        set { _description = value; }
    }

    private Int32 _priority;
    public Int32 Priority
    {
        get { return _priority; }
        set { _priority = value; }
    }

    public Task(String taskName, String description, Int32 priority)
    {
        this.TaskName = taskName;
        this.Description = description;
        this.Priority = priority;
    }
}

// This is a class that is a collection of Task types
// Since it inherits from ObservableCollection, it is itself a collection
// There is no need to declare/create an ArrayList inside.
// And on a strict note, do not ever use ArrayList. It is obsolete and not strongly typed.
// Use List<T>, ObservableCollection<T>, etc. instead.
// Look for more Generic Collections in System.Collections.Generic namespace
public class Tasks : ObservableCollection<Task>
{
    public Tasks()
    {
        Add(new Task("A foo task", "doing foo", 1));
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4451171

复制
相关文章

相似问题

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