绑定在WPF中的代码

时间:2017-01-05 23:01:46

标签: c# wpf xaml

如何在代码中添加绑定?

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

我尝试没有成功:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我找到了解决方案:

  GreenscreenEffect effect = new GreenscreenEffect() ;
                Binding binding = new Binding();
                binding.Path = new PropertyPath("Value");
                binding.Source = sliderGreenscreenTolerance; 
                BindingOperations.SetBinding(effect, GreenscreenEffect.ToleranceProperty, binding);