XAML样式,在DataTrigger上设置行为属性

时间:2012-11-16 14:48:36

标签: wpf styles

所以,我是WPF的新手,所以也许这是微不足道的,但我无法理解。

我有一个文本框。

<TextBox Text="{Binding NewRateAdjustment.Amount, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" Style="{StaticResource SurchargeAmountTextBox}" AttachedProperties:TextRules.TextRule ="{StaticResource numericRule}">
    <i:Interaction.Behaviors>
        <gl:NumericTextBoxBehavior DecimalLimit="2" />
    </i:Interaction.Behaviors>
</TextBox>

现在,我需要根据页面下拉菜单中的选项更改DecimalLimit,因此我创建了此样式。

<Style x:Key="SurchargeAmountTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=NewRateAdjustment.SelectedRateAdjustment.CalculationMethod.Name, UpdateSourceTrigger=PropertyChanged}" Value="Fuel">
            <Setter Property="Background" Value="Red"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=NewRateAdjustment.SelectedRateAdjustment.CalculationMethod.Name, UpdateSourceTrigger=PropertyChanged}" Value="">
            <Setter Property="Background" Value="Green"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

它似乎适用于颜色。但是如何为DecimalLimit写出Property Setter ???

1 个答案:

答案 0 :(得分:4)

您无法通过样式更改行为属性,但可以尝试通过样式应用行为。该主题已在其他问题中被解决,例如this,但在您的特定情况下,您不仅希望通过样式应用该行为,而且还希望根据数据应用不同的配置。 在下面的方法中,我将使用附加属性来实现它。 首先,虚拟行为类似于您正在使用的行为:

public class NumericTextBoxBehavior : Behavior<TextBox>
{
    public double DecimalLimit { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();

        // Dummy action so we can see the change when its applied
        this.AssociatedObject.Text = this.DecimalLimit.ToString();
    }
}

现在我们创建一个附加属性,它将负责应用行为(您可以在另一个类中执行此操作,或者如果您有权访问它,则可以在行为类中执行此操作):

public static class NumericTextBoxBehaviorExtension
{
    public static double? GetDecimalLimit(DependencyObject obj)
    {
        return (double?)obj.GetValue(DecimalLimitProperty);
    }
    public static void SetDecimalLimit(DependencyObject obj, double? value)
    {
        obj.SetValue(DecimalLimitProperty, value);
    }
    public static readonly DependencyProperty DecimalLimitProperty =
        DependencyProperty.RegisterAttached("DecimalLimit", typeof(double?), typeof(NumericTextBoxBehaviorExtension), new PropertyMetadata(null, OnDecimalLimitChanged));

    private static void OnDecimalLimitChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var behaviors = Interaction.GetBehaviors(sender);

        // Remove the existing behavior instances
        foreach (var old in behaviors.OfType<NumericTextBoxBehavior>().ToArray())
            behaviors.Remove(old);

        if (args.NewValue != null)
        {
            // Creates a new behavior and attaches to the target
            var behavior = new NumericTextBoxBehavior { DecimalLimit = (double)args.NewValue };

            // Apply the behavior
            behaviors.Add(behavior);
        }
    }
}

最后,以下测试用例将模拟您的场景。我们有一个TextBox样式,它将根据TextBox的DataContext的状态应用不同的DecimalLimit。 xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="TextBoxStyle" TargetType="TextBox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Name}" Value="Fuel">
                <Setter Property="local:NumericTextBoxBehaviorExtension.DecimalLimit" Value="10.0"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Name}" Value="">
                <Setter Property="local:NumericTextBoxBehaviorExtension.DecimalLimit" Value="1000.0"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="81,1,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Style="{StaticResource TextBoxStyle}"/>
</Grid>

在后面的代码中,我们将使按钮的操作交换TextBox的DataContext以验证该样式将正确更新行为:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var target = this.textBox1.DataContext as Target;
        if (this.textBox1.DataContext == null || string.IsNullOrEmpty(target.Name))
        {
            this.textBox1.DataContext = new Target() { Name = "Fuel" };
        }
        else
        {
            this.textBox1.DataContext = new Target() { Name = "" };
        }
    }
}

正如您所看到的,TextBox的Text将在每次交换DataContext时发生更改,这意味着样式确实正在执行正确的行为。