Example #1
0
        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SpriteCategoryComboBox control = (SpriteCategoryComboBox)d;

            if (control.supressEvents)
            {
                return;
            }
            control.supressEvents = true;
            string propName = string.Empty;

            if (control.GetBindingExpression(ItemsSourceProperty) != null)
            {
                propName = control.GetBindingExpression(ItemsSourceProperty).ResolvedSourcePropertyName;
            }
            if (control.ItemsSource != null)
            {
                Console.WriteLine($"{propName} SelectedItem Changed: Coerce");
                control.SelectedItemBinding = (ComboItem)OnCoerceSelectedItem(control, control.SelectedItemBinding);
            }
            else
            {
                Console.WriteLine($"{propName} SelectedItem Changed: null");
                control.SelectedItemBinding = null;
            }
            control.SelectedItemBinding = control.SelectedItem;
            control.supressEvents       = false;
        }
Example #2
0
        private static object OnCoerceSelectedItem(DependencyObject d, object baseValue)
        {
            SpriteCategoryComboBox control = (SpriteCategoryComboBox)d;

            /*ComboItem item = (ComboItem) baseValue;
             * if (item == null || !control.ItemsSource.Items.Contains(item)) {
             *      if (control.ItemsSource == null)
             *              return null;
             *      else
             *              return ((ComboItem) control.ItemsSource).Items.FirstOrDefault();
             * }
             * else {
             *      return baseValue;
             * }*/
            string propName = string.Empty;

            if (control.GetBindingExpression(SelectedItemProperty) != null)
            {
                propName = control.GetBindingExpression(SelectedItemProperty).ResolvedSourcePropertyName;
            }
            ComboItem item   = (ComboItem)baseValue;
            ComboItem source = control.ItemsSource;

            if (source == null)
            {
                Console.WriteLine($"{propName} Coerce: null");
                return(null);
            }
            else if (item != null && source.Items.Contains(item))
            {
                Console.WriteLine($"{propName} Coerce: Find");
                return(source.Items.First(i => i.Id == item.Id));
            }
            Console.WriteLine($"{propName} Coerce: FirstOrDefault");
            return(source.Items.FirstOrDefault());
        }