データ連結 > InputPanel と ObservableCollection の連結 |
InputPanel では、通常のコレクションと同様に機能する ObservableCollection を使用してコレクション連結を実装できます。InputPanel を ObservableCollection に連結するには、ObservableCollection<T> クラスを使用して、連結ソースとなるコレクションを取得します。次に、ItemsSource プロパティがこのコレクションを受け取って InputPanel コントロールに連結します。
ObservableCollection<T> クラスを使用してデータ連結を行うには、次の手順を実行します。
' ** フィールド Private m_id As Integer, cid As Integer Private m_first As String, m_last As String Private m_father As String Private m_occupation As EOccupation Private m_active As Boolean Private m_hired As DateTime Private m_weight As Double ' ** データの生成 Shared rnd As New Random() Shared firstNames As String() = "Andy|Ben|Charlie|Dan|Ed|Fred|Gil".Split("|"c) Shared lastNames As String() = "Ambers|Bishop|Cole|Danson|Evers|Trask|Ulam".Split("|"c) Shared countries As String() = "中国|インド|米国|日本|ミャンマー".Split("|"c)
// ** フィールド int id, cid; string first, last; string father; EOccupation occupation; bool active; DateTime hired; double weight; // ** データの生成 static Random rnd = new Random(); static string[] firstNames = "Andy|Ben|Charlie|Dan|Ed|Fred|Gil".Split('|'); static string[] lastNames = "Ambers|Bishop|Cole|Danson|EversTrask|Ulam".Split('|'); static string[] countries = "中国|インド|米国|日本|ミャンマー".Split('|');
Public Property ID() As Integer Get Return m_id End Get Set(value As Integer) If value <> m_id Then m_id = value End If End Set End Property Public ReadOnly Property 名前() As String Get Return String.Format("{0} {1}", 名, 姓) End Get End Property Public ReadOnly Property 国() As String Get Return countries(cid) End Get End Property Public Property 国ID() As Integer Get Return cid End Get Set(value As Integer) If value <> cid AndAlso value > -1 AndAlso value < countries.Length Then cid = value End If End Set End Property Public Property 職業() As EOccupation Get Return m_occupation End Get Set(value As EOccupation) If value <> m_occupation Then m_occupation = value End If End Set End Property Public Property アクティブ() As Boolean Get Return m_active End Get Set(value As Boolean) If value <> m_active Then m_active = value End If End Set End Property Public Property 名() As String Get Return m_first End Get Set(value As String) If value <> m_first Then m_first = value End If End Set End Property Public Property 姓() As String Get Return m_last End Get Set(value As String) If value <> m_last Then m_last = value End If End Set End Property Public Property 採用() As DateTime Get Return m_hired End Get Set(value As DateTime) If value <> m_hired Then m_hired = value End If End Set End Property Public Property 重量() As Double Get Return m_weight End Get Set(value As Double) If value <> m_weight Then m_weight = value End If End Set End Property ' 読み取り専用のもの Public ReadOnly Property 父親名() As String Get Return m_father End Get End Property ' ** ユーティリティー Private Shared Function GetString(arr As String()) _ As String Return arr(rnd.[Next](arr.Length)) End Function ' ** 静的値プロバイダ Public Shared Function GetCountries() As String() Return countries End Function Public Shared Function GetFirstNames() As String() Return firstNames End Function Public Shared Function GetLastNames() As String() Return lastNames End Function
public int ID { get { return id; } set { if (value != id) { id = value; } } } public string 名前 { get { return string.Format("{0} {1}", 名, 姓); } } public string 国 { get { return countries[cid]; } } public int 国ID { get { return cid; } set { if (value != cid && value > -1 && value < countries.Length) { cid = value; } } } public EOccupation 職業 { get { return occupation; } set { if (value != occupation) { occupation = value; } } } public bool アクティブ { get { return active; } set { if (value != active) { active = value; } } } public string 名 { get { return first; } set { if (value != first) { first = value; } } } public string 姓 { get { return last; } set { if (value != last) { last = value; } } } public DateTime 採用 { get { return hired; } set { if (value != hired) { hired = value; } } } public double 重量 { get { return weight; } set { if (value != weight) { weight = value; } } } // 読み取り専用のもの public string 父親名 { get { return father; } } // ** ユーティリティー static string GetString(string[] arr) { return arr[rnd.Next(arr.Length)]; } // ** 静的値プロバイダ public static string[] GetCountries() { return countries; } public static string[] GetFirstNames() { return firstNames; } public static string[] GetLastNames() { return lastNames; }
Private values As Array = [Enum].GetValues(GetType(EOccupation)) Public Sub New(id__1 As Integer) ID = id__1 名 = GetString(firstNames) 姓 = GetString(lastNames) 国ID = rnd.[Next]() Mod countries.Length 職業 = CType(values.GetValue(rnd.[Next](values.Length - 1)), EOccupation) アクティブ = rnd.NextDouble() >= 0.5 採用 = DateTime.Today.AddDays(-rnd.[Next](1, 365)) 重量 = 50 + rnd.NextDouble() * 50 m_father = String.Format("{0} {1}", GetString(firstNames), 姓) End Sub
Array values = Enum.GetValues(typeof(EOccupation)); public Employee(int id) { ID = id; 名 = GetString(firstNames); 姓 = GetString(lastNames); 国ID = rnd.Next() % countries.Length; 職業 = (EOccupation) (values.GetValue(rnd.Next(values.Length - 1))); アクティブ = rnd.NextDouble() >= .5; 採用 = DateTime.Today.AddDays(-rnd.Next(1, 365)); 重量 = 50 + rnd.NextDouble() * 50; father = string.Format("{0} {1}", GetString(firstNames), 姓); }
' ** 静的リストプロバイダ Public Shared Function GetEmployeeList(count As Integer) _ As ObservableCollection(Of Employee) Dim list = New ObservableCollection(Of Employee)() For i As Integer = 0 To count - 1 Dim emp As New Employee(i) list.Add(emp) Next Return list End Function
// ** 静的リストプロバイダ public static ObservableCollection<Employee> GetEmployeeList(int count) { var list = new ObservableCollection<Employee>(); for (int i = 0; i < count; i++) { Employee emp = new Employee(i); list.Add(emp); } return list; }
InPanel.ItemsSource = Employee.GetEmployeeList(50)
InPanel.ItemsSource = Employee.GetEmployeeList(50);