I would like to know if it's possible to make a condition on a textblock in a ListView. I explain :
I have a models which contains some data and there is an "amount" in this models. If the amount is negative I would like to put the foreground in red, and if it's positive I would like to put the foreground in green.
<TextBlock RelativePanel.AlignRightWithPanel="True"
Foreground="Red"
FontWeight="Bold">
<Run Text="{Binding Amount}" />
<Run Text="€" />
</TextBlock>
This is the textblock, he is in a ListView.ItemTemplate.
Regards,
Anthony
You should use a converter. Create a class (e.g. AmountColorConverter
) that derives from IValueConverter
.
public object Convert(object value, ...)
{
var val = (double)value;
return val >= 0
? Colors.Green
: Colors.Red;
}
Once implemented, create an instance of the converter in XAML and reference it in the binding:
<converter:AmountColorConverter x:Key="AmountColorConverter"/>
<TextBlock RelativePanel.AlignRightWithPanel="True"
Foreground="{Binding Amount, Converter={StaticResource AmountColorConverter}}"
FontWeight="Bold">
<Run Text="{Binding Amount}" />
<Run Text="€" />
</TextBlock>