2010-11-09 53 views
1

嗨 我有使用绑定来输入文本框背景颜色的问题。 我使用此代码如何将颜色绑定到文本框背景wpf

<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; 
     } 
    } 

当然我添加的类连接到DataContext的文本块

绑定 SolidColorBrush代替 Color

回答

5

的对象像下面。

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