msdn数据模板概述 [英] msdn Data Templating Overview

查看:80
本文介绍了msdn数据模板概述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm following msdn data templating overview http://msdn.microsoft.com/en-us/library/ms742521.aspx

但是我感到他们错过了解释某些东西的机会.

But I felt they missed explaining something..for resources they have:

 <Window.Resources><local:Tasks x:Key="myTodoList"/></Window.Resources>

它们在XAML中的全部是

And all they have in XAML is

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"/>

他们没有显示C#代码背后的内容,而是能够显示ListBox中的项目列表. ListBox没有x:Name,我无法在MainWindow中添加项目,并且使用单独的Tasks类,我做了以下工作(不起作用)

Without showing how C# code behind, they were able to show a list of items in the ListBox. The ListBox has no x:Name and I can't add the items in MainWindow, and with a separate class Tasks, I did the following (which doesn't work)

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;
        }
    }
}

我真的很困惑.

推荐答案

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

This should be the definition of Tasks for your sample to work correctly:

public class Tasks : ObservableCollection<Task /*or whatever type you want to use here*/>
{
    //...
}

-编辑-

// 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));
    }
}

这篇关于msdn数据模板概述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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