Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi all,

I've been trying to create a custom color picker and have got it working fairly well using some other examples and amending them. Here's the XAML:

XML
<UserControl x:Class="Customcontrols.Colorpicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib" Height="40" Width="200" Name="uccolorpicker"
             mc:Ignorable="d">
    <UserControl.Resources>
        
        <ResourceDictionary>
            <ObjectDataProvider MethodName="GetType" 
    ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
                <ObjectDataProvider.MethodParameters>
                    <sys:String>System.Windows.Media.Colors, PresentationCore,
            Version=3.0.0.0, Culture=neutral, 
            PublicKeyToken=31bf3856ad364e35</sys:String>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"  
    MethodName="GetProperties" x:Key="colorPropertiesOdp">
            </ObjectDataProvider>

        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ComboBox x:Name="superCombo" 
            Validation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTamplate}"
            ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" 
            SelectedValuePath="Name"
            SelectedValue="{Binding ElementName=uccolorpicker, Path=SelectedColor}" SelectionChanged="superCombo_SelectionChanged">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel x:Name="wpColours" Width="{Binding ActualWidth,
                        ElementName=superCombo}" Orientation="Horizontal" ItemHeight="15"
                        ItemWidth="20" Margin="2">                        
                    </WrapPanel>
                    
                </ItemsPanelTemplate>
                
            </ComboBox.ItemsPanel>
        <ComboBox.ItemTemplate>
            <DataTemplate>                    
                        <Rectangle x:Name="rect" Fill="{Binding Path=Name}" Height="{Binding ActualHeight,
                        ElementName=superCombo}" Width="{Binding ActualWidth,
                        ElementName=superCombo}"
                        RadiusX="0" RadiusY="0"/>
                </DataTemplate>
        </ComboBox.ItemTemplate> 
            
        </ComboBox>
    </Grid>
</UserControl>


And the code behind:

C#
public partial class Colorpicker : UserControl
    {
        public Colorpicker()
        {
            InitializeComponent();
        }

        public Brush SelectedColor
        {
            get { return (Brush)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(Colorpicker), new UIPropertyMetadata(null));
}


Now the problem II can't understand is when I try and set the initial selected value to a color in code behind so that it displays the correct color in "rect". If I do it as follows with a KnownColor enum type it works fine:

C#
superCombo.superCombo.SelectedValue = KnownColor.Red;


But if I try and do that with say this:

C#
superCombo.superCombo.SelectedValue = Colors.Plum;


It doesn't work. The SelectedValue stays as null.

Oooh, I almost forgot this part too:

XML
<Application.Resources>
        <ControlTemplate x:Key="ComboBoxValidationErrorTamplate">
            <DockPanel>
                <Border BorderBrush="Transparent" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Application.Resources>


Any help with this is muchly appreciated. Why can I use KnownColor or SystemColor to set the initial value, but not System.Windows.Media.Color? When the itemssource is a collection of that type?

Regards,

Jib
Posted
Updated 10-Aug-12 5:40am
v2
Comments
Kenneth Haugland 10-Aug-12 11:46am    
Its becouse its two different classes, nothing else :)
Jibrohni 14-Aug-12 3:24am    
Kenneth,

But the combobox shows the System.Windows.Media.Color selection as its itemssource, but manually changing to one of those colours in code doesn't change the selectedvalue properly.

KnownColor, that enumeration is very specific to WinForms, not a good idea to mix different color definitions of two different technologies unless you really have to which is not the case here.
Another thing is that in code you have to convert the color to a solid brush, since our are referring to brushes.
 
Share this answer
 
v2
Comments
Jibrohni 13-Aug-12 4:36am    
Yes, I would ideally like the user to be able to select any colour. The combobox shows all System.Windows.Media.Colors, but I can't set that in code and see it work like I can with KnownColors or ConsoleColors
Managed to set it on code successfully like so:

C#
PropertyInfo[] properties = typeof(Colors).GetProperties();
           int i = 0;
           foreach (PropertyInfo property in properties)
           {
               var color = property.GetValue(null, null);

               if(((System.Windows.Media.Color)color).A == Colors.PowderBlue.A &&
                      ((System.Windows.Media.Color)color).R == Colors.PowderBlue.R &&
                      ((System.Windows.Media.Color)color).G == Colors.PowderBlue.G &&
                      ((System.Windows.Media.Color)color).B == Colors.PowderBlue.B)
               {
                   superCombo.superCombo.SelectedIndex = i;
               }
               i++;
           }


Thanks for all your input and ideas,

Jib
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900