daniel
daniel

Reputation: 35733

Binding in code behind in WPF

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

Upvotes: 1

Views: 6517

Answers (2)

alek kowalczyk
alek kowalczyk

Reputation: 4934

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...

Upvotes: 2

daniel
daniel

Reputation: 35733

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

Upvotes: 0

Related Questions