新的扩展WPFToolkit ColorPicker

时间:2010-11-12 00:10:37

标签: wpf controls

我一直试图从我的应用程序中的工具包中获取新的颜色选择器,但没有成功......

以下是应该采用窗口背景颜色来填充当前颜色的示例代码,并且在新选择时,应该将背景颜色更改为所选颜色...

<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>

这是我在彩色标签上找到的所有文档...... http://elegantcode.com/2010/08/15/extended-wpf-toolkit-new-colorpicker-control/

5 个答案:

答案 0 :(得分:9)

这里的问题是Window.Background是Brush和SelectedColor,CurrentColor是Color。您可以使用转换器来实现此功能。

<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> 

和转换器

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

答案 1 :(得分:5)

转换功能对我来说不起作用,最终这就是诀窍:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return new SolidColorBrush((Color)value);
}

答案 2 :(得分:3)

谢谢Meleak。 discussions已经回答了这个问题。

最近也更新了ColorPicker。 Check it out.

答案 3 :(得分:0)

乍一看,这看起来是正确的。尝试在调试模式下运行应用程序,并在Visual Studio中观察“输出”窗口是否存在绑定错误。

答案 4 :(得分:0)

将设置用作中介。在Settings.settings中,创建string类型的用户范围参数。将其命名为BackColor1 然后为控件和元素的背景创建绑定,将它们都设置为相同的设置(如下所示)。优点是用户获得持久设置。我想要确切地说我测试它是一个网格行的背景,而不是一个窗口,但它应该工作相同。

 <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>