private static void OnSelectedColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ComboColorPicker cp = obj as ComboColorPicker;

            Debug.Assert(cp != null);

            Color newColor = (Color)args.NewValue;
            Color oldColor = (Color)args.OldValue;

            if (newColor == oldColor)
            {
                return;
            }

            // When the SelectedColor changes, set the selected value of the combo box
            ColorViewModel selectedColorViewModel = cp.ColorList1.SelectedValue as ColorViewModel;

            if (selectedColorViewModel == null || selectedColorViewModel.Color != newColor)
            {
                // Add the color if not found
                if (!cp.ListContains(newColor))
                {
                    cp.AddColor(newColor, newColor.ToString());
                }
            }

            // Also update the brush
            cp.SelectedBrush = new SolidColorBrush(newColor);
            cp.OnColorChanged(oldColor, newColor);
        }
Ejemplo n.º 2
0
        private void AddColor(Color color, string name)
        {
            if (!name.StartsWith("#", StringComparison.Ordinal))
            {
                name = NiceName(name);
            }
            var cvm = new ColorViewModel {
                Color = color, Name = name
            };

            ColorList1.Items.Add(cvm);
        }
 private bool ListContains(Color newColor)
 {
     foreach (object o in ColorList1.Items)
     {
         ColorViewModel vcm = o as ColorViewModel;
         if (vcm == null)
         {
             continue;
         }
         if (vcm.Color == newColor)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
 private void AddColor(Color color, string name)
 {
     if (!name.StartsWith("#",StringComparison.Ordinal))
         name = NiceName(name);
     ColorViewModel cvm = new ColorViewModel() { Color = color, Name = name };
     ColorList1.Items.Add(cvm);
 }