2010-12-15 21 views
1

私は、MSDNのデータテンプレートの概要にhttp://msdn.microsoft.com/en-us/library/ms742521.aspxMSDNデータテンプレートの概要

を以下のよしかし、私は、彼らは彼らが持っているsomething..forリソースを説明逃したと感じた:

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

そして彼らはXAMLで持っているすべては

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

C#のコードがどのように表示されているかを示さずに、ListBoxのアイテムのリストを表示できました。私は本当に混乱しています名前と私はメインウィンドウ内の項目を追加することはできませんし、別のクラスの作業で、私は

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

(動作しない)以下でした:リストボックスには、NO xを持っていません。

答えて

3

これが正しく動作するためにあなたのサンプルのためTasksの定義は次のようになります。

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

--EDIT--

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

+1、もしくは 'IListの'、または 'IBindingList'など – user7116

+0

ArrayListクラスにdatalist.Addを追加できません。アレイにどのように追加しますか? – KMC

+0

私はそれを取得しない、あなたはコードを提供できますか? – decyclone