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

<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中

最佳答案

绑定SolidColorBrush而不是Color,如下所示。

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

10-05 21:00
查看更多