コード例 #1
0
        private static ColorViewModel AddColor(Color color, string name)
        {
            if (!name.StartsWith("#", StringComparison.Ordinal))
            {
                name = NiceName(name);
            }
            var cvm = new ColorViewModel(color, name);

            m_colors.Add(cvm);

            return(cvm);
        }
コード例 #2
0
 private ColorViewModel ListContains(Color color)
 {
     foreach (object o in ColorList1.Items)
     {
         ColorViewModel vcm = o as ColorViewModel;
         if (vcm == null)
         {
             continue;
         }
         if (vcm.Color == color)
         {
             return(vcm);
         }
     }
     return(null);
 }
コード例 #3
0
        private static void OnSelectedColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ComboBoxColor cp = obj as ComboBoxColor;

            Debug.Assert(cp != null);

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


            //OnColorChanged(oldColor, newColor);

            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.Equals(newColor))
            {
                //cp.ColorList1.AutomationPropertiesName = selectedColorViewModel.Name;
            }

            if (selectedColorViewModel == null || !selectedColorViewModel.Color.Equals(newColor))
            {
                // Add the color if not found
                ColorViewModel cvm = cp.ListContains(newColor);
                if (cvm == null)
                {
                    cvm = AddColor(newColor, newColor.ToString());
                    //cp.ColorList1.Items.Add(cvm);
                    cp.ColorList1.Items.Refresh();
                    cp.ColorList1.SelectedIndex = cp.ColorList1.Items.Count - 1;
                }
                //cp.ColorList1.AutomationPropertiesName = cvm.Name;
            }

            // Also update the brush
            // cp.SelectedBrush = ColorBrushCache.Get(newColor);

            cp.SelectedColor = newColor;
        }