Wpf XAML样式,在DataTrigger上设置行为属性
所以,我是WPF的新手,所以这可能很琐碎,但我无法理解 我有一个文本框Wpf XAML样式,在DataTrigger上设置行为属性,wpf,styles,Wpf,Styles,所以,我是WPF的新手,所以这可能很琐碎,但我无法理解 我有一个文本框 <TextBox Text="{Binding NewRateAdjustment.Amount, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" Style="{StaticResource SurchargeAmountTextBox}" AttachedProper
<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>
现在,我需要根据页面下拉列表中的选择更改小数限制,所以我创建了这种样式
<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编写属性设置程序呢?您不能通过样式更改行为属性,但可以尝试通过样式应用行为。主题在其他问题中已中止,例如,但在您的特定情况下,您不仅希望通过样式应用行为,还希望根据数据使用不同的配置应用行为。 在下面的方法中,我将使用附加属性来实现这一点。 首先,与您正在使用的行为类似的虚拟行为:
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();
}
}
如您所见,每次交换DataContext时,文本框的文本都会发生变化,这意味着样式确实应用了正确的行为。您不能通过样式更改行为属性,但可以尝试通过样式应用行为。主题在其他问题中已中止,例如,但在您的特定情况下,您不仅希望通过样式应用行为,还希望根据数据使用不同的配置应用行为。 在下面的方法中,我将使用附加属性来实现这一点。 首先,与您正在使用的行为类似的虚拟行为:
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();
}
}
如您所见,每次交换DataContext时,文本框的文本都会发生变化,这意味着样式确实应用了正确的行为。它不是DependencyProperty。我已经尝试过向ViewModel添加一个属性并绑定到该属性。失败。让我们尝试使用附加属性,然后!请注意,在第二种方法中,附加属性将通过样式应用新的行为实例,而不是更改现有行为实例的小数限制。它应该可以工作,因为它本质上与工作示例的情况相同。还有一个问题,有没有办法让Value=Fuel和一个默认情况在它不是Fuel时使用?您可以在样式中为属性NumericTextBoxBehaviorExtension.DecimalLimit设置一个公共setter,并使用触发器在特定情况下对其进行更改。OnDecimalLimitChanged方法中的逻辑的实现方式应该删除该行为以前附加的任何实例,因此触发器中的setter应该覆盖样式setter。它不是dependencProperty。我已经尝试过向ViewModel添加一个属性并绑定到该属性。失败。让我们尝试使用附加属性,然后!请注意,在第二种方法中,附加属性将通过样式应用新的行为实例,而不是更改现有行为实例的小数限制。它应该可以工作,因为它本质上与工作示例的情况相同。还有一个问题,有没有办法让Value=Fuel和一个默认情况在它不是Fuel时使用?您可以在样式中为属性NumericTextBoxBehaviorExtension.DecimalLimit设置一个公共setter,并使用触发器在特定情况下对其进行更改。OnDecimalLimitChanged方法中的逻辑的实现方式应该删除之前附加的任何行为实例,因此触发器中的setter应该覆盖样式setter。
<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>
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 = "" };
}
}
}