开发者

New Extended WPFToolkit ColorPicker

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="WpfAppl开发者_Go百科ication1.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/


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);
        }
    }
}


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);
}


Thanks Meleak. This question has also been answered in the discussions.

Also the ColorPicker has recently been updated. Check it out.


That looks right at first glance. Try running your app in debug mode, and watch the Output window in Visual Studio for binding errors.


Use the settings as an intermediary. In your Settings.settings create a user scope parameter of type string. Name it BackColor1 Then create bindings for the control and for the element's background, both of them to the same setting (as below). The advantage is that the user gets a persistent setting. I want to precise that I tested it as a Grid row's background, not a window's but it should work the same.

 <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="{Binding Source={x:Static p:Settings.Default}, Path=BackColor1, Mode=TwoWay}">
    <Grid>
       <extToolkit:ColorPicker  
         SelectedColor="{Binding Source={x:Static p:Settings.Default}, Path=BackColor1, Mode=TwoWay}"/>
    </Grid>
</Window>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜