FREE TRIAL

|

☀
☾

ListView.SetDataSource

SetDataSource<T>(IList<T>)

Set the underlying data source. This instructs the list to start creating a 1-to-1 mapping of objects in the data source to ListView list items.

Remarks

Any desired Data.OnBind<TData> event handlers must be added via AddDataBinder<TData, TVisuals>(UIEventHandler<Data.OnBind<TData>, TVisuals, Int32>)
before this call to SetDataSource<T>(IList<T>). Otherwise the ListView won't know how to bind objects in the dataSource to any of the list item prefab types.

Declaration
public void SetDataSource<T>(IList<T> dataSource)
Parameters
Type Name Description
IList<T> dataSource

The list of data objects to bind to this ListView

Type Parameters
Name Description
T

The type of list element stored in dataSource

Examples
     using System;
     using System.Collections.Generic;
     using UnityEngine;

     // In Editor, assign and configure on an ItemView at the root of a toggle list-item prefab
     [Serializable]
     public class ToggleVisuals : ItemVisuals
     {
         // The visual to display a label string
         public TextBlock Label;

         // The visual to display toggle on/off state
         public UIBlock2D IsOnIndicator;
     }

     // The underlying data stored per toggle in a list of toggles
     [Serializable]
     public class ToggleData
     {
         public string Label;
         public bool IsOn;
     }

     public class ListViewBinder : MonoBehaviour
     {
          // Serialize and assign in the Editor
          public ListView ListView = null;

          // Serialize and assign in the Editor
          public List<ToggleData> Toggles = null;

          // Color to display when a toggle is "on"
          public Color ToggledOnColor = Color.blue;
          // Color to display when a toggle is "off"
          public Color ToggledOffColor = Color.grey;

          public void OnEnable()
          {
              // AddDataBinder<TData, TVisuals>(UIEventHandler<Data.OnBind<TData>, TVisuals, Int32>)
              ListView.AddDataBinder<ToggleData, ToggleVisuals>(BindToggle);
          }

          // Data.OnBind<TData>
          public void BindToggle(Data.OnBind<ToggleData> evt, ToggleVisuals fields, int index)
          {
              // Get the ToggleData off the Data.OnBind event
              ToggleData toggleData = evt.UserData;

              // Assign the ToggleVisuals.Label text to the underlying ToggleData.Label string
              fields.Label.Text = toggleData.Label;

              // Assign the ToggleVisuals.IsOnIndicator color to ToggledOnColor or ToggledOffColor, depending on the underlying ToggleData.IsOn bool
              fields.IsOnIndicator.Color = toggleData.IsOn ? ToggledOnColor : ToggledOffColor;
          }

          public void OnDisable()
          {
              // RemoveDataBinder<TData, TVisuals>(UIEventHandler<Data.OnBind<TData>, TVisuals, Int32>)
              ListView.RemoveDataBinder<ToggleData, ToggleVisuals>(BindToggle);
          }
     }
☀
☾
In This Article
Legal EmailContact Github
Copyright © 2022 Supernova Technologies, LLC