2017-11-01 26 views
-2

私はwpfで新しく、依存関係プロパティを学習しようとしています。 他のテキストボックスのテキストを使用して、テキストボックスの背景色を変更しようとしています。私はコンバータを使用してそれを行うことができますが、私はそれを依存プロパティを使用して実装したい。 はここにこの作業をされて罰金ブーティは、この使用してdependecyプロパティを実装するXAMLコード依存関係プロパティを使用して背景色を変更する方法

<TextBox Name="setbox" Width="150" Height="50" FontWeight="DemiBold" FontSize="25" Canvas.Top="50" Canvas.Left="10" 
       Background="{Binding ElementName=statusbar,Path=Text,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource converter1}}"/> 

と、この私のコンバータコード

public class backgroundColourConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo 
     culture) 
     { 
      var backColor = Brushes.Transparent; 
      //changes the colour of font to White if the input to the statusbar is "state1" 
      if (value != null && value.ToString().Equals("state1")) 
      { 
       backColor = Brushes.White; 
      } 
      //changes the colour of font to Lavender if the input to the statusbar is "state2" 
      else if (value != null && value.ToString().Equals("state2")) 
      { 
       backColor = Brushes.Lavender; 
      } 
      //changes the colour of font to Ivory if the input to the statusbar is "state3" 
      else if (value != null && value.ToString().Equals("state3")) 
      { 
       backColor = Brushes.Ivory; 
      } 
      //changes the colour of font to Green if the input to the statusbar is "state4" 
      else if (value != null && value.ToString().Equals("state4")) 
      { 
       backColor = Brushes.Green; 
      } 
      return backColor; 
     } 



     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

です。 ありがとうございました

+0

テキストは依存関係プロパティです。あなたは正確に何をしようとしていますか? – mm8

+0

カスタムの依存関係プロパティを使用して、テキストボックスの背景色を変更しようとしています。 – jithin

+0

カスタム作成した依存関係プロパティをポストします。 – mm8

答えて

1

独自のTextBoxコントロールを作成する場合、新しいプロパティBackgroundColorTextを追加し、色名を入力する他のTextBoxから値を設定できます。 BackgroundColorTextの設定では、コントロールの背景色を設定できます。

しかし、これはちょうど背景色を変更するためのビットオーバーキルです。それを行う適切な方法は、値コンバータ、imhoです。

関連する問題