2014-04-07 3 views
3

들어오는 이벤트를 기반으로 텍스트 상자의 전경색을 변경하려고합니다 (들어오는 수는 텍스트 상자의 수와 다릅니다). 텍스트가 변경되면 다시 검정색으로 변경합니다. UI를 통해. 나는 이것을 단호한 방식으로하고 있지만 올바른 방법은 확실치 않습니다.이벤트에서 WPF 텍스트 상자 전경색 변경

XAML : 뒤에

<TextBox Style="{StaticResource recParm}" Foreground="{Binding Path=AcquisitionTimeChangedByInstrument, Converter={StaticResource BooleanToBrush}}" Name="acquisitionTxtBox" TextChanged="onAcquisitionTimeChanged" > 
    <TextBox.Text> 
     <Binding Path="AcquisitionTime" Mode="TwoWay" StringFormat="{}{0:F6}" UpdateSourceTrigger="PropertyChanged" > 
      <Binding.ValidationRules> 
       <vm:AcquisitionTimeRule Min="200e-6" Max="40" /> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

코드 :

private void onAcquisitionTimeChanged(object sender, TextChangedEventArgs e) 
{ 
    //acquisitionTxtBox.Foreground = Brushes.Black; 
    ((ViewModel)Application.Current.Resources["vm"]).AcquisitionTimeChangedByInstrument = false; 
} 

AcquisitionTimeChangedByInstrument는 뷰 모델에하여 PropertyChanged을 제기 한 속성입니다. 변환기는 false의 경우 검정색으로 변경하고 true의 경우 파란색으로 변경합니다.

  1. 위의 양식에서 설명한대로 작동하는 것처럼 보이지만 이상하게 보입니다. 주석 처리 된 선을 사용하여 색상을 직접 변경하면 바인딩이 깨지는 것 같습니다. 즉, 뷰는 AcquisitionTimeChangedByInstrument에 대한 변경 사항 확인을 중지합니다. 왜?
  2. 이 작업을 수행하는 올바른 방법은 무엇입니까?

저는 며칠 동안 WPF 만 사용했음을 기억하십시오. 아직 고급 기능을 이해하지 못합니다.

(요청에 의해)

편집 결국 I는 텍스트 상자 값 AcquisitionTime 변경되지 않았는지 확인한다. 지금은 버튼을 클릭 할 때 단순히 AcquisitionTimeChangedByInstrument = true로 설정했습니다. PropertyChanged 이벤트를 보내지 만 이전에 콜백에서 acquisitionTxtBox.Foreground를 변경하지 않은 경우에만 get이 호출됩니다.

[ValueConversion(typeof(bool), typeof(SolidColorBrush))] 
public class BooleanToBrushConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (null == value) 
     { 
      return null; 
     } 
     if (value is bool) 
     { 
      if ((bool)value) 
      { 
       return (SolidColorBrush)Brushes.DeepSkyBlue; 
      } 
      return (SolidColorBrush)Brushes.Black; 
     } 

     Type type = value.GetType(); 
     throw new InvalidOperationException("No type " + type.Name); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

사용? 변환기 코드뿐만 아니라 조건을 제공 할 수 있습니까? 나는 당신을 도울 수 있습니다. –

답변

2

로컬에서 종속 특성 설정은 다른 설정보다 우선 할 수 있습니다. 효과적으로 바인딩을 덮어 씁니다. 포어 그라운드 색상을 변경하기위한 조건은 무엇인가

acquisitionTxtBox.SetCurrentValue(ForegroundProperty, Brushes.Black); 

Blog entry explaining setcurrentvalue