Any way to include a Color chooser, picker or palette in a custom dock?

Hey there,

I'm working on a more feature-rich color replacer. I may be an idiot to ask, but is there a way to add a color picker, chooser and palette to a StackPanel in a custom dock? The XAML designer in Visual Studio offers buttons, date pickers, canvases, etc. but nothing close to a color picker.

  • Search forum for "Colorreplacer". You can find alternative versions of a replacer macro with a picker etc.

  • When I've created custom menus that require icons in Visual Studio, I used small JPG images next to radio button controls. You could create color swatches as JPGs.  

  • Former Member
    0 Former Member over 3 years ago

    You can create own color picker

    XAML

    <Window x:Class="ColorPickerTest.ColorPicker"
                 xmlns="">schemas.microsoft.com/.../presentation"
                 xmlns:x="">schemas.microsoft.com/.../xaml"
                 xmlns:mc="">schemas.openxmlformats.org/.../2006" 
                 xmlns:d="">schemas.microsoft.com/.../2008" 
                 xmlns:local="clr-namespace:ColorPickerTest"
                 mc:Ignorable="d" 
                 Height="400" Width="166" ResizeMode="NoResize" ShowInTaskbar="False" WindowStyle="None">
        <Border BorderBrush="#FF535050" BorderThickness="1" Padding="2">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
            <Label Content="{Binding PaletteName}" Grid.Row="0"/>
            <ItemsControl  Name="Colors" Grid.Row="1" ItemsSource="{Binding ColorArray}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Background="{Binding ColorHexValue}" Width="20" Height="20" 
                                    CommandParameter="{Binding}" Command="{Binding Path=DataContext.ColorSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}">
                            <Button.ToolTip>
                                <ToolTip>
                                    <StackPanel>
                                        <Label  Content="{Binding CorelColorName}" FontSize="14" FontWeight="Bold" />
                                        <Label Content="{Binding ColorHexValue}" FontSize="12" />
                                    </StackPanel>
                                </ToolTip>
                            </Button.ToolTip>
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <StackPanel Grid.Row="2" Orientation="Horizontal">
                <Rectangle Fill="{Binding SelectedColor.ColorHexValue}" Width="20" Height="20" HorizontalAlignment="Left" Margin="2,0,0,0"/>
                <Button Content="Ok" IsDefault="True" Name="btn_ok" Width="60" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="8,0,0,0"/>
                <Button Content="Cancel" IsCancel="True" Name="btn_cancel" Width="60" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="8,0,0,0"/>
            </StackPanel>       
        </Grid>
        </Border>
    </Window>
    

    CSharp

    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;
    
    namespace ColorPickerTest
    {
        /// <summary>
        /// Interaction logic for ColorPicker.xaml
        /// </summary>
        public partial class ColorPicker : Window
        {
            ColorManager colorManager;
            public ColorPicker(Corel.Interop.VGCore.Palette palette)
            {
                InitializeComponent();
                colorManager = new ColorManager(palette);
                this.DataContext = colorManager;
                btn_ok.Click += (s, e) => { this.DialogResult = true; };
            }
            public ColorSystem SelectedColor { get { return colorManager.SelectedColor; } }
            protected override void OnDeactivated(EventArgs e)
            {
                try
                {
                    this.Close();
                }
                catch { }
            }
            protected override void OnLostFocus(RoutedEventArgs e)
            {
               this.Close();
            }
        }
    
    }
    public class RoutedCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public Action<T> action;
    
    
        public RoutedCommand(Action<T> action)
        {
            this.action = action;
        }
    
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public void Execute(object parameter)
        {
            action.Invoke((T)parameter);
        }
    }
    public class ColorManager : INotifyPropertyChanged
    {
        private ColorSystem[] colorArray;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ColorSystem[] ColorArray { get { return this.colorArray; } }
        public RoutedCommand<ColorSystem> ColorSelected { get { return new RoutedCommand<ColorSystem>(SetSelectedColor); } }
    
        private ColorSystem selectedColor;
        public ColorSystem SelectedColor
        {
            get
            {
                return selectedColor;
            }
            set
            {
                selectedColor = value;
                NotifyPropertyChanged();
            }
        }
        public string PaletteName { get; set; }
        private void SetSelectedColor(ColorSystem color)
        {
            SelectedColor = color;
        }
        public ColorManager(Corel.Interop.VGCore.Palette palette)
        {
            if (palette == null)
                return;
            PaletteName = palette.Name;
            colorArray = new ColorSystem[palette.ColorCount];
            for (int i = 1; i < palette.ColorCount; i++)
            {
                Corel.Interop.VGCore.Color color = palette.Color[i];
                colorArray[i - 1] = new ColorSystem(color.HexValue, color.Name, color);
            }
    
        }
        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
    }
    public class ColorSystem
    {
        public ColorSystem(string colorHexValue, string corelColorName, Corel.Interop.VGCore.Color corelColor)
        {
            this.colorHexValue = colorHexValue;
            this.corelColorName = corelColorName;
            this.corelColor = corelColor;
        }
    
        private string colorHexValue;
    
        public string ColorHexValue
        {
            get { return colorHexValue; }
            set { colorHexValue = value; }
        }
        private string corelColorName;
    
        public string CorelColorName
        {
            get { return corelColorName; }
            set { corelColorName = value; }
        }
        private Corel.Interop.VGCore.Color corelColor;
        public Corel.Interop.VGCore.Color CorelColor
        {
            get { return corelColor; }
            set { corelColor = value; }
        }
    
    
    }
    

    Usage

     ColorPicker c = new ColorPicker(corelApp.ActivePalette);
                    if((bool)c.ShowDialog())
                    {
                        Color color = c.SelectedColor.CorelColor;
                    }
    

    Result