Edw*_*uay 44 wpf xaml datatrigger datatemplate
以下DataTemplate.DataTrigger使年龄显示为红色,如果它等于 30.
如果年龄显示大于 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>
Run Code Online (Sandbox Code Playgroud)
Mik*_*nen 72
你可以创建一个IValueConverter,它根据整数将整数转换为布尔值CutOff.然后使用DataTrigger.Value的True(或者False,这取决于你正在返回什么).
DataTrigger如果我没记错的话,WPF 是严格的相等比较器.
所以类似于:
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; }
}
Run Code Online (Sandbox Code Playgroud)
然后使用以下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>
Run Code Online (Sandbox Code Playgroud)
Dre*_*hie 11
我建议使用一个IValueConverter绑定到ForegroundAge 的元素TextBlock并在那里隔离着色逻辑.
<TextBlock x:Name="Age"
Text="{Binding Age}"
Foreground="{Binding Path=Age, Converter={StaticResource AgeToColorConverter}}" />
Run Code Online (Sandbox Code Playgroud)
然后在代码中:
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
Ωme*_*Man 10
我相信有一种更简单的方法可以通过使用MVVM和MV的功能来实现目标INotifyPropertyChanged.
使用该Age属性创建另一个属性,该属性将被称为布尔值IsAgeValid.这IsAgeValid只是一个按需检查,在技术上不需要OnNotify通话.怎么样?
要将更改推送到Xaml,请将OnNotifyPropertyChangedfor IsAgeValid放在Agesetter中.
任何绑定都IsAgeValid将在任何Age更改上发送通知消息,这实际上是正在查看的内容.
一旦设置,当然将样式触发器绑定为false,并相应地IsAgeValid结果为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.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51191 次 |
| 最近记录: |