如何将颜色绑定到文本框背景wpf

时间:2010-11-09 17:55:43

标签: wpf binding colors background

您好 我在使用绑定设置文本框背景颜色时遇到问题。 我用这个代码

<TextBlock Width="Auto" Height="Auto"
                                   Text="{Binding ConnectionType}"
                                   Canvas.Left="{Binding LabelPosition.X}"
                                   Canvas.Top="{Binding LabelPosition.Y}" Background="{Binding ParentCanvasColor}">

                          <TextBlock.RenderTransform>
                            <TranslateTransform X="5" Y="5"/>
                          </TextBlock.RenderTransform>
                        </TextBlock>

ParentCanvasColoris属性,在我的类中称为连接。此属性看起来像

 public Color ParentCanvasColor
    {
        get
        {
            if (parentCanvas != null && parentCanvas is DesignerCanvasNetDiag)
            {
                return Colors.Red;
            }
            return Colors.Transparent;
        }
    }

当然,我将类Connection的对象添加到textBlock的datacontext

1 个答案:

答案 0 :(得分:5)

绑定SolidColorBrush代替Color,如下所示。

    public SolidColorBrush ParentCanvasColor
    {
        get
        {
            if (parentCanvas != null && parentCanvas is DesignerCanvasNetDiag)
            {
                return  new SolidColorBrush(Colors.Red);
            }
            return  new SolidColorBrush(Colors.Transparent);
        }
    }
相关问题