2012-01-10 44 views
2

我試圖找出一種方式來應用樣式到文本框元素,當它不包含文本。我希望TextBox在其包含或不包含任何文本時具有不同的背景色(例如)。將樣式應用到Windows Phone文本框時,它不包含文本

由於觸發器不是我可以在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

創建一個轉換器綁定到文本屬性,風格 – 2012-01-10 20:05:50

回答

1

這應該是相當容易使用自定義的行爲來實現。使用this link創建可以附加到TextBox控件的行爲。在OnAttached方法中,您可以處理即使是LostFocus方法的TextChanged事件,以檢查文本框是否爲空。因此,您可以在樣式之間切換樣式。

P.S:您可能需要在更改樣式後調用TextBox.ApplyTemplate()方法。請注意,雖然。

+0

之間互換這個答案讓我在正確的方向,所以我將其標記爲答案。我使用的解決方案在第一篇文章中。 – RajenK 2012-01-11 07:49:45

1

創建您的樣式

<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" /> 

創建轉換

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}}" />