データ連結 > InputPanel と CollectionView の連結 |
InputPanel では、レコード管理、フィルタ処理、グループ化、ソートの各機能を備えたインタフェース ICollectionView を使用して、コレクション連結を実装できます。InputPanel を ObservableCollection に連結するために、ICollectionView インタフェースを実装するオブジェクトに InputPanel を連結できます。以下の例では、ObservableCollection<T> クラスを連結ソースとして使用してコレクションを取得し、ICollectionView インタフェースを実装する CollectionView クラスを使用してソースコレクションを表示しています。 その後、C1InputPanel クラスの ItemsSource プロパティを使用して、InputPanel コントロールを ICollectionView に連結しています。
ICollectionView を使用してデータ連結を行うには、次の手順を実行します。
Shared lines As String() = "コンピュータ|ワッシャ|ストーブ".Split("|"c) Shared colors As String() = "赤|緑|青|白".Split("|"c)
static string[] lines = "コンピュータ|ワッシャ|ストーブ".Split('|'); static string[] colors = "赤|緑|青|白".Split('|');
Public Property Line() As String Get Return DirectCast(GetValue("Line"), String) End Get Set(value As String) SetValue("Line", value) End Set End Property Public Property Color() As String Get Return DirectCast(GetValue("Color"), String) End Get Set(value As String) SetValue("Color", value) End Set End Property Public Property Name() As String Get Return DirectCast(GetValue("Name"), String) End Get Set(value As String) SetValue("Name", value) End Set End Property Public Property Price() As Double Get Return CDbl(GetValue("Price")) End Get Set(value As Double) SetValue("Price", value) End Set End Property Public Property Weight() As Double Get Return CDbl(GetValue("Weight")) End Get Set(value As Double) SetValue("Weight", value) End Set End Property Public Property Cost() As Double Get Return CDbl(GetValue("Cost")) End Get Set(value As Double) SetValue("Cost", value) End Set End Property Public Property Volume() As Double Get Return CDbl(GetValue("Volume")) End Get Set(value As Double) SetValue("Volume", value) End Set End Property Public Property Discontinued() As Boolean Get Return CBool(GetValue("Discontinued")) End Get Set(value As Boolean) SetValue("Discontinued", value) End Set End Property Public Property Rating() As Integer Get Return CInt(GetValue("Rating")) End Get Set(value As Integer) SetValue("Rating", value) End Set End Property ' 値を取得または設定します Private values As New Dictionary(Of String, Object)() Private Function GetValue(p As String) As Object Dim value As Object values.TryGetValue(p, value) Return value End Function Private Sub SetValue(p As String, value As Object) If Not Object.Equals(value, GetValue(p)) Then values(p) = value OnPropertyChanged(p) End If End Sub Public Shared Function GetLines() As String() Return lines End Function
[Display(Name = "製品")] public string Line { get { return (string)GetValue("Line"); } set { SetValue("Line", value); } } [Display(Name = "色")] public string Color { get { return (string)GetValue("Color"); } set { SetValue("Color", value); } } [Display(Name = "名前")] public string Name { get { return (string)GetValue("Name"); } set { SetValue("Name", value); } } [Display(Name = "価格")] public double Price { get { return (double)GetValue("Price"); } set { SetValue("Price", value); } } [Display(Name = "重量")] public double Weight { get { return (double)GetValue("Weight"); } set { SetValue("Weight", value); } } [Display(Name = "原価")] public double Cost { get { return (double)GetValue("Cost"); } set { SetValue("Cost", value); } } [Display(Name = "量")] public double Volume { get { return (double)GetValue("Volume"); } set { SetValue("Volume", value); } } [Display(Name = "廃止")] public bool Discontinued { get { return (bool)GetValue("Discontinued"); } set { SetValue("Discontinued", value); } } [Display(Name = "評価")] public int Rating { get { return (int)GetValue("Rating"); } set { SetValue("Rating", value); } } // 値を取得または設定します Dictionary<string, object> values = new Dictionary<string, object>(); object GetValue(string p) { object value; values.TryGetValue(p, out value); return value; } void SetValue(string p, object value) { if (!object.Equals(value, GetValue(p))) { values[p] = value; OnPropertyChanged(p); } } protected virtual void OnPropertyChanged(string p) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(p)); } public static string[] GetLines() { return lines; }
Public Shared Function GetProducts(count As Integer) As IEnumerable Dim list = New ObservableCollection(Of Products)() Dim rnd = New Random(0) For i As Integer = 0 To count - 1 Dim p = New Products() p.Line = lines(rnd.[Next]() Mod lines.Length) p.Color = colors(rnd.[Next]() Mod colors.Length) p.Name = _ String.Format("{0} {1}{2}", _ p.Line.Substring(0, p.Line.Length - 1), _ p.Line(0), i) p.Price = (rnd.[Next](1, 1000) + _ rnd.[Next](1, 1000) + _ rnd.[Next](1, 1000)) / 3 p.Weight = (rnd.[Next](1, 100) + _ rnd.[Next](1, 100) + _ rnd.[Next](1, 300)) / 5 p.Cost = rnd.[Next](1, 600) p.Volume = rnd.[Next](500, 5000) p.Discontinued = rnd.NextDouble() < 0.1 p.Rating = rnd.[Next](0, 5) list.Add(p) Next Return list End Function
public static IEnumerable GetProducts(int count) { var list = new ObservableCollection<Products>(); var rnd = new Random(0); for (int i = 0; i < count; i++) { var p = new Products(); p.Line = lines[rnd.Next() % lines.Length]; p.Color = colors[rnd.Next() % colors.Length]; p.Name = string.Format("{0} {1}{2}", p.Line.Substring(0, p.Line.Length - 1), p.Line[0], i); p.Price = (rnd.Next(1, 1000) + rnd.Next(1, 1000) + rnd.Next(1, 1000)) / 3; p.Weight = (rnd.Next(1, 100) + rnd.Next(1, 100) + rnd.Next(1, 300)) / 5; p.Cost = rnd.Next(1, 600); p.Volume = rnd.Next(500, 5000); p.Discontinued = rnd.NextDouble() < .1; p.Rating = rnd.Next(0, 5); list.Add(p); } return list; }
Private Shared view As ICollectionView Public Shared ReadOnly Property _ CustomerCollectionView() As ICollectionView Get If view Is Nothing Then Dim products__1 = Products.GetProducts(50) view = New CollectionView(products__1) End If Return view End Get End Property
private static ICollectionView view; public static ICollectionView CustomerCollectionView { get { if (view == null) { var products = Products.GetProducts(50); view = new CollectionView(products); } return view; } }
InPanel.ItemsSource = CustomerCollectionView
InPanel.ItemsSource = CustomerCollectionView;