Question

I need to bind the SelectedColor property of ColorPicker to a custom color which is not present in available colors. I created a simple test to show my problem. My xaml:

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

Code behind (CurrentStyle.PenColor returns an integer value which equals 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:

enter image description here

Though, when I go to Advanced colors I can see that the color has been recognized and set correctly. Here is a pic:

enter image description here

Hope for your help. Thanks a lot!

EDIT

I implemented INotifyPropertyChanged, still to no avail. 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.

Était-ce utile?

La solution

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.

EDIT

The values you are calculating from PenColor are (0, 200, 10, 5), which is transparent and hence shown correctly. Did you mean (255, 200, 10, 5) which is red?

Autres conseils

You must implement INotifyPropertyChanged And raise PropertyChanged event with the name of "Test"

MSDN has an example

INotifyPropertyChanged

This allows WPF to be notified when your property changes

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top