【问题标题】:How Do I bind a Color to a SolidColorBrush in code behind如何在后面的代码中将颜色绑定到 SolidColorBrush
【发布时间】:2013-11-22 06:10:13
【问题描述】:

您可以将 SolidColorBrush 绑定到后面代码中的颜色吗?我希望能够做到这一点:

Binding binding = new Binding("FontColor");
binding.Source = this;
SolidColorBrush brush = new SolidColorBrush();
brush.SetBinding(SolidColorBrush.ColorProperty, binding);

我知道画笔不是 BindingExpressionBase,所以它没有 SetBinding() 方法。但是您可以在 xaml 中设置绑定。后面的代码是怎么做到的?

【问题讨论】:

    标签: c# wpf binding brush


    【解决方案1】:

    尝试使用System.Windows.Data.BindingOperations.SetBinding

    BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, binding);
    

    【讨论】:

    • 效果很好。谢谢。
    【解决方案2】:
    Background = new SolidColorBrush(Colors.Red);
    

    【讨论】:

    • 您直接绑定到控件上的属性,我认为这不是 OP 所要求的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多