对不起基本问题。如何在以下代码中简化multiple if条件?:
<Window.Resources>
<YurNs:GreaterThanValConverter x:Key="GreaterThanValConverter"/>
</Window.Resources>
<StackPanel>
<TextBox Text="{Binding Amount,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="Button">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Amount,Converter={StaticResource GreaterThanValConverter}}" Value="false">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
答案 0 :(得分:2)
请你更具体一点吗?你什么意思更简单? 使其更简单(或实际上更具可读性)的一种方法是:
var acceptedValues = ["val1", "val2"];
var x = "foo"
if(acceptedValues.indexOf(x) >= 0){
alert ('Hello World');
}
答案 1 :(得分:2)
您可以使用正则表达式。
if (/^val[1-2]$/.test(x))
alert("Hello World");