简体   繁体   English

我如何在后面的代码中将颜色绑定到SolidColorBrush

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

相关问题 如何将ResourceDictionary中的SolidColorBrush的Color属性绑定到ViewModel Color属性 - How could I bind a SolidColorBrush's Color property in a ResourceDictionary to a ViewModel Color property 如何在 WPF 后面的代码中绑定属性 - How Do I Bind Properties in Code Behind WPF 将SolidColorBrush的颜色绑定到Button的背景色 - Bind color of SolidColorBrush to background color of Button 如何将System.Windows.Media.SolidcolorBrush转换为System.Drawing.Color? - How do I convert System.Windows.Media.SolidcolorBrush to System.Drawing.Color? 无法在XAML中将属性与SolidColorBrush的颜色属性绑定 - Unable to bind property with color attribute of SolidColorBrush in xaml 如何使用 DynamicResource 更新 SolidColorBrush 颜色? - How to update SolidColorBrush color with a DynamicResource? WPF:如何将窗口的 ShowInTaskbar 属性绑定到代码隐藏值? - WPF: How do I bind a window's ShowInTaskbar property to code-behind value? 如何在代码后面绑定到Canvas.Left或Canvas.Top附加属性? - How do I bind to Canvas.Left or Canvas.Top attached property in code behind? 如何将基于键的字典值绑定到Code-Behind中的TextBlock? - How do I bind Dictionary Value based on Key to a TextBlock in Code-Behind? 如何防止SolidColorBrush对象的内存泄漏? - How do I prevent memory leak from SolidColorBrush object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM