繁体   English   中英

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

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

我需要将ColorPicker的SelectedColor属性绑定到可用颜色中不存在的自定义颜色。 我创建了一个简单的测试来显示我的问题。 我的xaml:

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

后面的代码(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();

  }

这就是加载窗口时我的ColorPicker的样子:

在此处输入图片说明

但是,当我转到“高级颜色”时,可以看到该颜色已被识别并正确设置。 这是一张照片:

在此处输入图片说明

希望对您有所帮助。 非常感谢!

编辑

我实现了INotifyPropertyChanged,仍然无济于事。 这是代码:

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

也许我在这里做错了。

从发布的代码中很难判断这是否是您的问题,但是Test的类型必须是System.Windows.Media.Color而不是System.Background.Color

编辑

您从PenColor计算的值是(0,200,10,5),它是透明的,因此可以正确显示。 您是说(255,200,10,5)红色吗?

您必须实现INotifyPropertyChanged并引发名称为“ Test”的PropertyChanged事件。

MSDN有一个例子

INotifyPropertyChanged的

这样可以在属性更改时通知WPF

暂无
暂无

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

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