Hi,
I would like to know how to make a string color when signalTest: FAIL display in the grid ListBox should show as red color and PASS as green color and battery with percentage in C# with color, too (see code below):
XAML
<Grid HorizontalAlignment="Stretch" Name="uxGlance" Visibility="Collapsed" Grid.RowSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="20"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label x:Name="uxSensorGlanceTitle" Height="38" FontSize="21.333" VerticalAlignment="Top" Content="At a Glance" FontWeight="Bold" Foreground="#FF325C7F" FontStyle="Italic"/>
<Border Height="1" BorderBrush="Black" BorderThickness="0,1,0,0" VerticalAlignment="Top" Margin="0,35,10,0"/>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="55" SharedSizeGroup="A"/>
<ColumnDefinition Width="*" SharedSizeGroup="B"/>
<ColumnDefinition Width="100" SharedSizeGroup="C"/>
<ColumnDefinition Width="*" MinWidth="100" SharedSizeGroup="D"/>
<ColumnDefinition Width="125" SharedSizeGroup="E"/>
<ColumnDefinition Width="100" SharedSizeGroup="F"/>
<ColumnDefinition Width="100" SharedSizeGroup="G"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="Sensor Name" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Column="2" Text="Version" HorizontalAlignment="Right" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Column="3" Text="Data" Margin="0,0,10,0" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Column="4" Text="Last Check In" Margin="0,0,10,0" HorizontalAlignment="Right" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Column="5" Text="Signal" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold" />
<TextBlock Grid.Column="6" Text="Battery" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"/>
</Grid>
<ListBox Grid.Row="2" Name="uxSensorList" ItemsSource="{Binding}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.IsSharedSizeScope="False">
<ListBox.Resources>
<DataTemplate DataType="{x:Type business:Sensor}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="1"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" SharedSizeGroup="A"/>
<ColumnDefinition Width="*" SharedSizeGroup="B"/>
<ColumnDefinition Width="100" SharedSizeGroup="C"/>
<ColumnDefinition Width="*" MinWidth="100" SharedSizeGroup="D"/>
<ColumnDefinition Width="125" SharedSizeGroup="E"/>
<ColumnDefinition Width="100" SharedSizeGroup="F"/>
<ColumnDefinition Width="100" SharedSizeGroup="G"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{Binding Path=SensorName}" FontWeight="Bold" VerticalAlignment="Top">
<LineBreak/>
</TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Path=RadioBand}" FontWeight="Bold" VerticalAlignment="Bottom">
</TextBlock>
<Label Grid.Column="2" HorizontalAlignment="Right" Content="{Binding Path=FirmwareVersion}" VerticalAlignment="Center" />
<TextBlock Grid.Column="3" Text="{Binding Path=SensorID, Converter={StaticResource LastReading}}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Column="4" Text="{Binding Path=LastCommunicationDate, Converter={StaticResource ToLocalDateTime}}" FontSize="13" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Column="5" Content="{Binding Path=SensorID, Converter={StaticResource SignalSTR}}" HorizontalAlignment="Right" VerticalAlignment="Center" />
<Label Grid.Column="6" HorizontalAlignment="Center" Content="{Binding Path=SensorID, Converter={StaticResource BatteryRead}}" VerticalAlignment="Center"/>
<Rectangle Grid.Row="1" Grid.ColumnSpan="7" Stroke="#FF000000" Height="1" StrokeThickness="1" StrokeDashArray="4 4" SnapsToDevicePixels="True"/>
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
namespace MonatDesktop
{
public class LastReadingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Sensor sens = Sensor.Load(value.ToLong());
if (sens == null)
return "";
DataMessage message = DataMessage.LoadLastBySensorQuick(sens.SensorID, sens.LastDataMessageGUID, sens.LastCommunicationDate);
if (message != null)
{
return message.DisplayData.Replace(",", "\r\n");
//return message.DisplayData.Replace(",", "\r\n") + " | Raw :" + message.Data;
}
else
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class LastSignalSTR : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Sensor sens = Sensor.Load(value.ToLong());
if (sens == null)
return "";
DataMessage message = DataMessage.LoadLastBySensorQuick(sens.SensorID, sens.LastDataMessageGUID, sens.LastCommunicationDate);
if (message != null)
{
//here Fail and Pass
int maxRSSI = int.MinValue;
string signalTest = " FAIL ";
//--- here string signalTest FAIL in color Red
//signalTest = System.Drawing.Color.Red;
if (string.IsNullOrEmpty(sens.GenerationType) || sens.GenerationType == "Gen1")
{
maxRSSI = -40;
}
if (sens.GenerationType == "Gen2")
{
if (sens.RadioBand.Contains("433"))
maxRSSI = -40;
else
maxRSSI = -35;
}
if (maxRSSI <= message.SignalStrength)
signalTest = " PASS ";
//- HERE to make string signalTest: PASS display in green color
//Here display PASS or FAIL (-23 / -75 ) and message in orange color
return signalTest + "(" + message.SignalStrength.ToString() + "/" + maxRSSI.ToString() + ")";
}
else
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Lastbatterrd : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Sensor sens = Sensor.Load(value.ToLong());
if (sens == null)
return "";
DataMessage message = DataMessage.LoadLastBySensorQuick(sens.SensorID, sens.LastDataMessageGUID, sens.LastCommunicationDate);
if (message != null)
{
return message.Battery;
//- converting / or making battery should display in percentage with any color
}
else
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}