1

How can I add a binding in code behind?

 <Canvas.Effect >
      <fx:GreenscreenEffect Tolerance="{Binding Value, ElementName=sliderGreenscreenTolerance}" 
                            ColorR="{Binding Value, ElementName=sliderGreenscreenR}"
                            ColorG="{Binding Value, ElementName=sliderGreenscreenG}"
                            ColorB="{Binding Value, ElementName=sliderGreenscreenB}" />
 </Canvas.Effect>

I tried without success:

GreenscreenEffect effect = new GreenscreenEffect() ;
Binding binding = new Binding();
binding.Path = new PropertyPath("Tolerance");
binding.Source = sliderGreenscreenTolerance.Value; 
BindingOperations.SetBinding(effect, TextBlock.TextProperty, binding);
// etc. for each property
abatishchev
  • 98,240
  • 88
  • 296
  • 433
daniel
  • 34,281
  • 39
  • 104
  • 158

2 Answers2

2
GreenscreenEffect effect = new GreenscreenEffect() ;
Binding binding = new Binding();
binding.Path = new PropertyPath("Value");
binding.Source = sliderGreenscreenTolerance; 
// effect.SetBinding(GreenscreenEffect.ToleranceProperty, binding);
// Commented above out since GreenscreenEffect is not a FrameworkElement, thus:
BindingOperations.SetBinding(effect, GreenscreenEffect.ToleranceProperty, binding);
// ... ColorRProperty etc...
alek kowalczyk
  • 4,896
  • 1
  • 26
  • 55
0

I found the solution:

  GreenscreenEffect effect = new GreenscreenEffect() ;
                Binding binding = new Binding();
                binding.Path = new PropertyPath("Value");
                binding.Source = sliderGreenscreenTolerance; 
                BindingOperations.SetBinding(effect, GreenscreenEffect.ToleranceProperty, binding);
daniel
  • 34,281
  • 39
  • 104
  • 158
  • you found it as I was typing :) just letting know that you can use the `SetBinding` method on the control instead of the one in `BindingOperations` – alek kowalczyk Jan 05 '17 at 23:11