2017-11-01 20 views
-2

저는 wpf에서 새로운데 종속성 속성을 배우려고합니다. 다른 텍스트 상자의 텍스트를 사용하여 텍스트 상자의 배경색을 변경하려고합니다. 나는 변환기를 사용하여 그것을 할 수 있지만 종속 속성을 사용하여 구현 싶어요. 여기이 작동하고 좋은 일 뿐이이 사용하는 실행 종속 속성을 구현하고자하는 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의 setter에서 컨트롤 배경색을 설정할 수 있습니다.

하지만 배경색을 수정하기에는 다소 과잉입니다. 그것을하는 적당한 방법은 가치 변환기, imho입니다.