简体   繁体   English

在WPF中绑定代码

[英]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
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...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM