Exemple #1
0
        public static GridColumn AddDropDownColumn <T>(this GridView view, Func <T, object> value, IEnumerable <object> dataStore, IIndirectBinding <string> textBinding, string header, bool editable = false)
        {
            var  internalBinding = Binding.Delegate(value);
            Cell cell;

            // hack for eto/gtk not supporting ItemTextBinding on ComboBoxCells
            if (Platform.Instance.IsGtk)
            {
                const char zws = '\u200B'; // This is a zero width (ZWS) space Unicode character.

                var lDataStore = dataStore.ToList();

                var tDataStore = lDataStore.Select(i =>
                                                   string.Empty.PadLeft(lDataStore.IndexOf(i) + 1, zws) // Pad with index+1 ZWS chars to be able to check later. If no ZWS is there, this did not work.
                                                   + textBinding.GetValue(i))
                                 .ToList();
                var hasNonUnique = tDataStore.Distinct().Count() != tDataStore.Count; // Check if textBinding produced the same string for more than one data item.

                var binding = Binding.Delegate <T, string>(
                    (T s) => textBinding.GetValue(internalBinding.GetValue(s)),
                    (T t, string s) =>
                {
                    int idx = s.Split(zws).Length - 2;
                    if (idx == -1)
                    {
                        idx = tDataStore.IndexOf(s);     // Fallback if ZWS is not supported.
                        if (hasNonUnique)
                        {
                            throw new Exception("ComboBoxCell ComboTextBinding Polyfill: Duplicate text entry encountered and Zero-Width-Space Hack not supported by platform!");
                        }
                    }

                    internalBinding.SetValue(t, lDataStore[idx]);
                }).Cast <object>();

                cell = new ComboBoxCell {
                    Binding = binding, DataStore = tDataStore
                };
            }
            else
            {
                cell = new ComboBoxCell {
                    Binding = internalBinding, DataStore = dataStore, ComboTextBinding = textBinding
                }
            };

            return(view.AddColumn(cell, header, editable));
        }