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

los*_*eko 2 wpf binding background colors

嗨,我有一个问题,使用绑定设置文本框背景颜色.我用这个代码

<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>
Run Code Online (Sandbox Code Playgroud)

ParentCanvasColoris属性,在我的类中称为连接.这个属性看起来像那样

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

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

Zai*_*ikh 6

绑定SolidColorBrush而不是Color像下面那样.

    public SolidColorBrush ParentCanvasColor
    {
        get
        {
            if (parentCanvas != null && parentCanvas is DesignerCanvasNetDiag)
            {
                return  new SolidColorBrush(Colors.Red);
            }
            return  new SolidColorBrush(Colors.Transparent);
        }
    }
Run Code Online (Sandbox Code Playgroud)