wpf 如何让 DataTemplate.DataTrigger 检查大于或小于?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get DataTemplate.DataTrigger to check for greater than or less than?
提问by Edward Tanguay
The following DataTemplate.DataTrigger
makes the age display red if it is equal to30.
DataTemplate.DataTrigger
如果年龄等于30 ,则以下使年龄显示为红色。
How do I make the age display red if it is greater than30?
如果年龄大于30,如何使年龄显示为红色?
<DataTemplate DataType="{x:Type local:Customer}">
<Grid x:Name="MainGrid" Style="{StaticResource customerGridMainStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" Margin="5"/>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" Margin="5"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" Margin="5"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" Margin="5"/>
<TextBlock Grid.Column="0" Grid.Row="2" Text="Age" Margin="5"/>
<TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Margin="5"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Age}">
<DataTrigger.Value>30</DataTrigger.Value>
<Setter TargetName="Age" Property="Foreground" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
回答by Mikko Rantanen
You could create an IValueConverter
, which converts an integer to a boolean based on the CutOff
. Then use DataTrigger.Value
of True
(or False
, depending on what you are returning).
您可以创建一个IValueConverter
,它根据 将整数转换为布尔值CutOff
。然后使用DataTrigger.Value
的True
(或者False
,这取决于你正在返回什么)。
WPF DataTrigger
s are strictly equality comparers if I remember correctly.
DataTrigger
如果我没记错的话,WPF是严格相等的比较器。
So something similar to:
所以类似于:
public class CutoffConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return ((int)value) > Cutoff;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
public int Cutoff { get; set; }
}
Then use the following XAML.
然后使用以下 XAML。
<Window.Resources>
<myNamespace:CutoffConverter x:Key="AgeConverter" Cutoff="30" />
</Window.Resources>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Age,
Converter={StaticResource AgeConverter}}">
<DataTrigger.Value>true</DataTrigger.Value>
<Setter TargetName="Age" Property="Foreground" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
回答by Drew McGhie
I'd recommend using an IValueConverter
to bind to the Foreground
element of the Age TextBlock
and isolating the coloring logic there.
我建议使用 anIValueConverter
来绑定到Foreground
Age的元素TextBlock
并在那里隔离着色逻辑。
<TextBlock x:Name="Age"
Text="{Binding Age}"
Foreground="{Binding Path=Age, Converter={StaticResource AgeToColorConverter}}" />
Then in the Code:
然后在代码中:
[ValueConversion(typeof(int), typeof(Brush))]
public class AgeToColorConverter : IValueConverter
{
public object Convert(object value, Type target)
{
int age;
Int32.TryParse(value.ToString(), age);
return (age >= 30 ? Brushes.Red : Brushes.Black);
}
}
回答by ΩmegaMan
I believe there is a simpler way of acheiving the goal by using the powers of MVVM and INotifyPropertyChanged
.
我相信有一种更简单的方法可以通过使用 MVVM 和INotifyPropertyChanged
.
With the Age
property create another property which will be a boolean called IsAgeValid
. The IsAgeValid
will simply be an on demandcheck which does not technicallyneed an the OnNotify
call. How?
使用该Age
属性创建另一个属性,该属性将是一个名为IsAgeValid
. 这IsAgeValid
将只是一个按需检查,技术上不需要OnNotify
呼叫。如何?
To get changes pushed to the Xaml, place the OnNotifyPropertyChanged
event to be fired for IsAgeValid
withinthe Age
setter instead.
要将更改推送到 Xaml,请将OnNotifyPropertyChanged
要触发的事件IsAgeValid
放置在Age
setter 中。
Any binding to IsAgeValid
will alsohave a notify message sent on any Age
change subscriptions; which is what really is being looked at...
任何结合IsAgeValid
将还具有通知上的任何发送的消息Age
的变化订阅; 这才是真正被关注的......
Once setup, of course bind the style trigger for false and true accordingly to the IsAgeValid
result.
设置后,当然会根据IsAgeValid
结果将样式触发器绑定为 false 和 true 。
public bool IsAgeValid{ get { return Age > 30; } }
public int Age
{
get { return _Age; }
set
{
_Age=value;
OnPropertyChanged("Age");
OnPropertyChanged("IsAgeValid"); // When age changes, so does the
// question *is age valid* changes. So
// update the controls dependent on it.
}
}
回答by Mark
If possible, you could add a property to your model, that's the easiest way. Eg.
如果可能,您可以向模型添加属性,这是最简单的方法。例如。
public int AgeBoundry
{
get
{
if (Age < 30)
return 0;
else if (Age == 30)
return 1;
else
return 2;
}
}
then you can check on the value of the integer.
然后你可以检查整数的值。
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Age}">
<DataTrigger.Value>0</DataTrigger.Value>
<Setter TargetName="Age" Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Age}">
<DataTrigger.Value>1</DataTrigger.Value>
<Setter TargetName="Age" Property="Foreground" Value="Orange"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Age}">
<DataTrigger.Value>2</DataTrigger.Value>
<Setter TargetName="Age" Property="Foreground" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>