当样式不包含文本时,将样式应用于Windows Phone TextBox

时间:2012-01-10 19:47:29

标签: silverlight windows-phone-7

我正在尝试找到一种方法,在TextBox元素不包含文本时将其应用于TextBox元素。我希望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>

2 个答案:

答案 0 :(得分:1)

使用自定义行为应该很容易实现。使用this link创建可以附加到TextBox控件的行为。在OnAttached方法中,您可以处理TextChanged方法的LostFocus事件,以检查TextBox是否为空。因此,您可以在样式之间切换样式。

P.S:您可能需要在更改样式后调用TextBox.ApplyTemplate()方法。请注意确定。

答案 1 :(得分: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}}"  />
相关问题