简体   繁体   English

如何将自定义颜色绑定到WPF工具包ColorPicker?

[英]How can I bind a custom color to WPF toolkit ColorPicker?

I need to bind the SelectedColor property of ColorPicker to a custom color which is not present in available colors. 我需要将ColorPicker的SelectedColor属性绑定到可用颜色中不存在的自定义颜色。 I created a simple test to show my problem. 我创建了一个简单的测试来显示我的问题。 My xaml: 我的xaml:

<xctk:ColorPicker SelectedColor="{Binding Path=Test}"></xctk:ColorPicker>

Code behind (CurrentStyle.PenColor returns an integer value which equals 13109765 ): 后面的代码(CurrentStyle.PenColor返回一个等于13109765的整数值):

  public Color Test
  {
      get;
      set;
  }

  public MyClass()
  {
     DataContext = this;

     Test = Color.FromArgb((byte)((CurrentStyle.PenColor >> 24) & 0xFF),
               (byte)((CurrentStyle.PenColor >> 16) & 0xFF),
               (byte)((CurrentStyle.PenColor >> 8) & 0xFF),
               (byte)(CurrentStyle.PenColor & 0xFF));
     InitializeComponent();

  }

And that's how my ColorPicker looks like when the window is loaded: 这就是加载窗口时我的ColorPicker的样子:

在此处输入图片说明

Though, when I go to Advanced colors I can see that the color has been recognized and set correctly. 但是,当我转到“高级颜色”时,可以看到该颜色已被识别并正确设置。 Here is a pic: 这是一张照片:

在此处输入图片说明

Hope for your help. 希望对您有所帮助。 Thanks a lot! 非常感谢!

EDIT 编辑

I implemented INotifyPropertyChanged, still to no avail. 我实现了INotifyPropertyChanged,仍然无济于事。 Here's the code: 这是代码:

public Color Test
    {
        get
        {
            return test;
        } 
        set
        {
            if (test != value)
            {
                test = value;
                OnPropertyChanged("Test");
            }
       }
    }


public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string prop)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }

Maybe I'm doing smth wrong here. 也许我在这里做错了。

Difficult to tell if this is your issue from the posted code but the type of Test has to be System.Windows.Media.Color rather than System.Background.Color . 从发布的代码中很难判断这是否是您的问题,但是Test的类型必须是System.Windows.Media.Color而不是System.Background.Color

EDIT 编辑

The values you are calculating from PenColor are (0, 200, 10, 5), which is transparent and hence shown correctly. 您从PenColor计算的值是(0,200,10,5),它是透明的,因此可以正确显示。 Did you mean (255, 200, 10, 5) which is red? 您是说(255,200,10,5)红色吗?

You must implement INotifyPropertyChanged And raise PropertyChanged event with the name of "Test" 您必须实现INotifyPropertyChanged并引发名称为“ Test”的PropertyChanged事件。

MSDN has an example MSDN有一个例子

INotifyPropertyChanged INotifyPropertyChanged的

This allows WPF to be notified when your property changes 这样可以在属性更改时通知WPF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM