Request: Dropdown example

Issue #37 resolved
Rainer Bernhardt created an issue

Can you maybe create an example how to use dropdown list with databinding.

The problem is, I would like to set the available options via binding, the current option and update the data context when a option was changed.

Thank you

Comments (7)

  1. Christian Oeing repo owner

    Hey Rainer! Did you have a look at the EnumEqualityCheck example? It uses a drop down list to choose from the enum values. Maybe that already helps?

  2. Rainer Bernhardt reporter

    I did, but this is a custom implementation of a dropdown list. So there is no way to bind the normal unity dropdown component?

  3. Christian Oeing repo owner

    Ah, I see. There is no setter for the option data yet, but I started to put together an example for the next release.

    In the mean time you can use this setter:

    namespace Slash.Unity.DataBind.UI.Unity.Setters
    {
        using System.Collections.Generic;
    
        using Slash.Unity.DataBind.Foundation.Setters;
    
        using UnityEngine;
        using UnityEngine.UI;
    
        /// <summary>
        ///   Set the options of a dropdown element to the provided list of option data items.
        /// </summary>
        [AddComponentMenu("Data Bind/UnityUI/Setters/[DB] Dropdown Options Setter (Unity)")]
        public class DropdownOptionsSetter : ComponentSingleSetter<Dropdown, IEnumerable<Dropdown.OptionData>>
        {
            /// <inheritdoc />
            protected override void UpdateTargetValue(Dropdown target, IEnumerable<Dropdown.OptionData> value)
            {
                target.ClearOptions();
                if (value != null)
                {
                    target.AddOptions(new List<Dropdown.OptionData>(value));
                }
            }
        }
    }
    

    You still need to provide a list or collection of Dropdown.OptionData (see below) in the context, but I will try to provide a data converter for lists in the next release. Hope that helps :)

            private readonly Property<Collection<Dropdown.OptionData>> optionDataListProperty
                =
                new Property<Collection<Dropdown.OptionData>>(
                    new Collection<Dropdown.OptionData>());
    
            public Collection<Dropdown.OptionData> OptionDataList
            {
                get
                {
                    return this.optionDataListProperty.Value;
                }
                set
                {
                    this.optionDataListProperty.Value = value;
                }
            }
    
  4. Christian Oeing repo owner

    I added an example to use Data Bind with a Unity UI dropdown. It will be released with the next version (1.0.9)

    Furthermore there will be a generic way to convert the items of a collection to a different type (e.g. Dropdown.OptionData), so you are not forced to use Dropdown.OptionData in your data context.

  5. Log in to comment