I designed a battery level indicator consisting of a Border inside another Border.
The Width of inner Border is Multi-Bound to the outer Border ActualWidth
and the battery value itself (from DataContext
, ranging from 0.0 to 1.0):
<Border x:Name="BatteryChargeContainer">
<Border x:Name="BatteryCharge" Margin="1" HorizontalAlignment="Left" Background="Gray">
<Border.Width>
<MultiBinding Converter="{StaticResource NormalValueConverter}" FallbackValue="10">
<Binding Path="BatteryLevel"/>
<Binding Path="ActualWidth" ElementName="BatteryChargeContainer"/>
</MultiBinding>
</Border.Width>
</Border>
</Border>
==================
class NormalValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.Any(v => v == null))
return null;
var v1 = values[0];
var v2 = values[1];
double valor = System.Convert.ToDouble(v1);
double measure = System.Convert.ToDouble(v2);
return valor * measure;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Now I want to make something similar for the Background
property, with another IMultiValueConverter. The color should be calculated like this:
- Green if above 0.8;
- Yellow between 0.6 and 0.8;
- Orange between 0.3 and 0.6;
- Red if below 0.3.
These colors are defined as Resources, because I absolutely don't want to declare them in the code, so my question is:
If the colors are defined as resources, and the actual applied color is calculated in the MultiBind Converter, how can I apply color based on the value returned, or else have the MultiConverter assess the available colors and return the selected color itself?