0

My boss had downloaded a xaml control that I must use in app. It looks like ok but there is a strange trouble with inner logic - the property CurrentColor (that I need to use in our app) is defined in control.xaml.cs file like :

public SolidColorBrush CurrentColor
{
     get { return (SolidColorBrush)GetValue(CurrentColorProperty); }
     set
     {
          SetValue(CurrentColorProperty, value);
          ActiveColor = CurrentColor;
     }
}

I am using this control in my dialog (that has its own view model class) and I am writing this kind of binding:

CurrentColor="{Binding myOwnViewModel.ColorActualValue, Mode=Default, UpdateSourceTrigger=PropertyChanged}">

in myOwnViewModel.cs (that implements INotifyPropertyChanged) I have my property

public SolidColorBrush ColorActualValue{ // here is some logic}

But when I debug an app I have never target MY CurrentColor - I am always go to the CurrentColor from control.xaml.cs

How could I bind this "third party" control to my property from my ViewModel?

Perhaps this is because (control.xaml.cs):

  public static DependencyProperty CurrentColorProperty =
            DependencyProperty.Register("CurrentColor", typeof(SolidColorBrush), typeof(ColorPickerComboBox), new PropertyMetadata(Brushes.Chocolate));
        public static RoutedEvent ActiveColorChangedEvent = EventManager.RegisterRoutedEvent("ActiveColorChanged",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColorPickerComboBox)); 

I have found the question What's wrong with "DataContext = this" in WPF user controls? and removed DataContext = this; from constructor of this control - but this still not helped

Community
  • 1
  • 1
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • Set the datacontext to ur viewmodel and then remove the viewmodel from ur binding declaration – Brandon Seydel Jul 28 '15 at 12:12
  • 1
    You're binding to a property myOwnViewModel.ColorActualValue but in your ViewModel it names CurrentColor. Check the output window for binding errors. – GreenEyedAndy Jul 28 '15 at 12:20
  • GreenEyedAndy - sorry for this mistake I have corrected the question – curiousity Jul 28 '15 at 12:27
  • Can you provide some link to the Control project/site/full code? – Francesco De Lisi Jul 28 '15 at 14:31
  • How is your DataContext for `ColorPickerComboBox set, and what is it? The binding of `myOwnViewModel.ColorActualValue` looks weird, as it means you're binding to `ColorPickerComboBox.DataContext.myOwnViewModel.ColorActualValue`, and I suspect the `myOwnViewModel` is either a mistake or not public. Most likely you are trying to bind to `ColorPickerComboBox.DataContext.ColorActualValue`, where the DataContext is `myOwnViewModel`. If that's the case, your binding should just be `{Binding ColorActualValue}` – Rachel Jul 28 '15 at 15:24

1 Answers1

0

Should the binding look like this?

CurrentColor="{Binding ColorActualValue, Mode=Default, UpdateSourceTrigger=PropertyChanged}">

The DataContext of your 'dialog' needs to be the ViewModel class that contains the ColorActualValue property

public Dialog()
{
    DataContext = new myOwnViewModel();
}
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • no, I have tried this. Any way thank you for the answer and take a look at my updated question – curiousity Jul 28 '15 at 13:21
  • I could not pass an instance of myOwnViewModel to Dialog constructor - firstly Dialog is in another project, secondly myOwnViewModel is not static class and has parameters that not present in the project where the Dialog is located. I will try to write a proper code after InitializeComponent in the place where myOwnViewModel is available (to change the control to another one with proper DataContext)but I think that this is a bad practise – curiousity Jul 28 '15 at 13:44
  • Yes doesn't sound good. See how some sample applications are put together – Glen Thomas Jul 28 '15 at 13:47