2010-11-09 126 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); 
     } 
    }