Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Extended WPFToolkit ColorPicker

Tags:

wpf

controls

I've been trying to get the new colorpicker from the toolkit working in my app, without success...

Here is sample code that is supposed to pick up the color of the window background to populate current color, and upon new selection, is supposed to change the background color to the selected color...

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200" xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
        Name="Window" Background="blue">
    <Grid>
        <extToolkit:ColorPicker Name="colorPicker1" 
                                SelectedColor="{Binding ElementName=Window,Path=Background}" 
                                CurrentColor="{Binding ElementName=Window,Path=Background}" />
    </Grid>
</Window>

This is all the documentation I've been able to locate on the colorpicker... http://elegantcode.com/2010/08/15/extended-wpf-toolkit-new-colorpicker-control/

like image 743
Jeff Main Avatar asked Nov 12 '10 00:11

Jeff Main


2 Answers

The problem here is that Window.Background is a Brush and SelectedColor and CurrentColor is Color. You can get this to work by using a Converter.

<Window x:Class="WpfApplication1.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="100" Width="200"   
        Name="Window" Background="blue">
    <Window.Resources>
        <local:BrushColorConverter x:Key="BrushColorConverter"/>
    </Window.Resources>
    <Grid>
        <extToolkit:ColorPicker Name="colorPicker1"  
                                SelectedColor="{Binding ElementName=Window, 
                                    Path=Background, 
                                    Converter={StaticResource BrushColorConverter}}"
                                CurrentColor="{Binding ElementName=Window, 
                                    Path=Background, 
                                    Converter={StaticResource BrushColorConverter}}" />
    </Grid>
</Window> 

And the Converter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication1
{
    public class BrushColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            SolidColorBrush brush = value as SolidColorBrush;
            return brush.Color;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }
    }
}
like image 130
Fredrik Hedblad Avatar answered Nov 05 '22 21:11

Fredrik Hedblad


The Convert function wasn't working for me, eventually this did the trick:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new SolidColorBrush((Color)value);
}
like image 42
ChaseTheSun Avatar answered Nov 05 '22 21:11

ChaseTheSun