2012-01-10 6 views
2

TextBox要素にテキストが含まれていないときにスタイルを適用する方法を見つけようとしています。 TextBoxにテキストが含まれていない場合や、テキストが含まれていない場合など、TextBoxの背景色を変えたいとします。Windows PhoneのTextBoxにテキストが含まれていないときにスタイルを適用します。

トリガーは私がSilverlight(afaik)で使用できるものではないので、これを行う別の方法はありますか?この動作のためだけに、TextBoxのカスタム実装を記述するのが好きではありません。ありがとう。


私は、デフォルトの動作(ConditionBehavior)を使用して終了:

<i:Interaction.Triggers> 
    <i:EventTrigger EventName="TextChanged"> 
     <i:Interaction.Behaviors> 
      <ec:ConditionBehavior> 
       <ec:ConditionalExpression> 
        <ec:ComparisonCondition LeftOperand="{Binding Text, ElementName=textBox}" RightOperand="" Operator="NotEqual"/> 
       </ec:ConditionalExpression> 
      </ec:ConditionBehavior> 
     </i:Interaction.Behaviors> 
     <ec:ChangePropertyAction PropertyName="Background" Value="{StaticResource PhoneTextBoxBrush}" /> 
    </i:EventTrigger> 
    <i:EventTrigger EventName="TextChanged"> 
     <i:Interaction.Behaviors> 
      <ec:ConditionBehavior> 
       <ec:ConditionalExpression> 
        <ec:ComparisonCondition LeftOperand="{Binding Text, ElementName=textBox}" RightOperand="" Operator="Equal"/> 
       </ec:ConditionalExpression> 
      </ec:ConditionBehavior> 
     </i:Interaction.Behaviors> 
     <ec:ChangePropertyAction PropertyName="Background" Value="Transparent" /> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 
+0

を作成

<Style x:Key="FilledStyle" TargetType="TextBox"> <Setter Property="Background" Value="Beige" /> </Style> <Style x:Key="EmptyStyle" TargetType="TextBox"> <Setter Property="Background" Value="Yellow" /> </Style> <Models:TextStyleConverter x:Key="TextStyleConverter" /> 

あなたのスタイルを作成スタイル –

答えて

1

これは、カスタム動作を実現することはかなり簡単にする必要があります。 this linkを使用して、TextBoxコントロールにアタッチできるビヘイビアを作成します。 OnAttachedメソッドでは、LostFocusメソッドのTextChangedイベントを処理して、TextBoxが空であるかどうかを確認することができます。したがって、スタイル間でスタイルを切り替えることができます。

P.S:You は、スタイルを変更した後にTextBox.ApplyTemplate()メソッドを呼び出す必要があります。しかしそれについて確かに注意してください。

+0

間のスワップこの答えはで私を助けたことを、テキストプロパティにバインドコンバータを作成します。私はこれを答えとしてマークしています。私が使った解決策は、最初の投稿にあります。 – RajenK

1

コンバータ

public class TextStyleConverter : IValueConverter 
    { 
    #region Implementation of IValueConverter 

    /// <summary> 
    /// Modifies the source data before passing it to the target for display in the UI. 
    /// </summary> 
    /// <returns> 
    /// The value to be passed to the target dependency property. 
    /// </returns> 
    /// <param name="value">The source data being passed to the target.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param> 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var val = value as string; 
     if (String.IsNullOrEmpty(val)) return Application.Current.Resources["EmptyStyle"]; 

     return Application.Current.Resources["FilledStyle"]; 
    } 

    /// <summary> 
    /// Modifies the target data before passing it to the source object. This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings. 
    /// </summary> 
    /// <returns> 
    /// The value to be passed to the source object. 
    /// </returns> 
    /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param> 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

使用

<TextBox x:Name="locationtbx" Style="{Binding ElementName=locationtbx, Path=Text, Converter={StaticResource TextStyleConverter}}" />