[英]How Do I bind a Color to a SolidColorBrush in code behind
Can you bind a SolidColorBrush to a color in code behind? 您可以将SolidColorBrush绑定到后面代码中的颜色吗? I want to be able to do this:
我希望能够做到这一点:
Binding binding = new Binding("FontColor");
binding.Source = this;
SolidColorBrush brush = new SolidColorBrush();
brush.SetBinding(SolidColorBrush.ColorProperty, binding);
I know the brush is not a BindingExpressionBase, so it does not have a SetBinding() method. 我知道画笔不是BindingExpressionBase,因此它没有SetBinding()方法。 But you can set a binding in xaml.
但是您可以在xaml中设置绑定。 How do you do it in code behind?
您如何在后面的代码中做到这一点?
尝试使用System.Windows.Data.BindingOperations.SetBinding
:
BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, binding);
Background = new SolidColorBrush(Colors.Red);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.