Data Binding / Binding InputPanel with CollectionView
Binding InputPanel with CollectionView

Collection binding can be implemented in InputPanel using ICollectionView, an interface with record management, filtering, grouping, and sorting functionalities. To bind InputPanel to an ObservableCollection, InputPanel can be bound to an object that implements the ICollectionView interface. In the following example, we have used the ObservableCollection<T> class as a binding source to obtain the collection and the C1CollectionView class that implements the ICollectionView interface to display the source collection. Later, bind the InputPanel control to the ICollectionView using ItemsSource property of C1InputPanel class.

Perform the following steps for data binding using ICollectionView:

  1. Set up the application
  2. Create a data source for InputPanel
  3. Bind InputPanel to ICollectionView

Set up the application

  1. Create a UWP application.
  2. Add InputPanel control to the application and name it InPanel.
Back to Top

Create a data source for InputPanel

  1. Add a new class, Product.cs, to the application.
  2. Add the following fields to the class.
    Shared lines As String() = "Computers|Washers|Stoves".Split("|"c)
    Shared colors As String() = "Red|Green|Blue|White".Split("|"c)
    
  3. Add the following properties and methods to the class.
        <Display(Name:="Line")>
        Public Property Line() As String
            Get
                Return DirectCast(GetValue("Line"), String)
            End Get
            Set
                SetValue("Line", Value)
            End Set
        End Property
    
        <Display(Name:="Color")>
        Public Property Color() As String
            Get
                Return DirectCast(GetValue("Color"), String)
            End Get
            Set
                SetValue("Color", Value)
            End Set
        End Property
    
        <Display(Name:="Name")>
        Public Property Name() As String
            Get
                Return DirectCast(GetValue("Name"), String)
            End Get
            Set
                SetValue("Name", Value)
            End Set
        End Property
    
        <Display(Name:="Price")>
        Public Property Price() As Double
            Get
                Return CDbl(GetValue("Price"))
            End Get
            Set
                SetValue("Price", Value)
            End Set
        End Property
    
        <Display(Name:="Weight")>
        Public Property Weight() As Double
            Get
                Return CDbl(GetValue("Weight"))
            End Get
            Set
                SetValue("Weight", Value)
            End Set
        End Property
    
        <Display(Name:="Cost")>
        Public Property Cost() As Double
            Get
                Return CDbl(GetValue("Cost"))
            End Get
            Set
                SetValue("Cost", Value)
            End Set
        End Property
    
        <Display(Name:="Volume")>
        Public Property Volume() As Double
            Get
                Return CDbl(GetValue("Volume"))
            End Get
            Set
                SetValue("Volume", Value)
            End Set
        End Property
    
        <Display(Name:="Discontinued")>
        Public Property Discontinued() As Boolean
            Get
                Return CBool(GetValue("Discontinued"))
            End Get
            Set
                SetValue("Discontinued", Value)
            End Set
        End Property
    
        <Display(Name:="Rating")>
        Public Property Rating() As Integer
            Get
                Return CInt(GetValue("Rating"))
            End Get
            Set
                SetValue("Rating", Value)
            End Set
        End Property
    
        ' get/set values
        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
        Protected Overridable Sub OnPropertyChanged(p As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(p))
        End Sub
    
        Public Shared Function GetLines() As String()
            Return lines
        End Function
    
    
    #Region "INotifyPropertyChanged Members"
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
  4. Create a method, GetProducts, of IEnumerable interface using the following code.
    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
    
  5. Add the following code to create a property, CustomerCollectionView, of ICollectionView interface which uses the C1CollectionView class to display the source collection.
    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 C1CollectionView(products__1)
            End If
            Return view
        End Get
    End Property
    
Back to Top

Bind InputPanel to ICollectionView

  1. Add the following code to bind the InputPanel control with data using the ItemsSource property.
    InPanel.ItemsSource = CustomerCollectionView
    
  2. Press F5 to run the application.
Back to Top
See Also