WPF DataTemplate DataTrigger
<Window x:Class="WpfApp79.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp79" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:GeneralForecastToBrushConverter x:Key="gf2brush"/> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition /> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <TextBlock Text="Select number of days to forecast:" FontSize="15" VerticalAlignment="Center" Margin="4"/> <ComboBox x:Name="_days" SelectedIndex="0" Width="50"/> <Button Margin="4" Content="Get Forecast" FontSize="16" Click="Button_Click" /> </StackPanel> <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="5" TextBlock.FontSize="15"> <TextBlock Text="Select units:" Margin="4"/> <RadioButton Content="Celsius" IsChecked="True" Margin="10,5"/> <RadioButton Content="Fahrenheit" Margin="20,5"/> </StackPanel> <ListBox ItemsSource="{Binding}" Grid.Row="2" HorizontalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Border x:Name="_border" Margin="5" BorderBrush="Black" Padding="5" BorderThickness="2" Background="LightGray"> <StackPanel Orientation="Horizontal"> <TextBlock FontSize="20" FontWeight="ExtraBold" Text="{Binding GeneralForeCastValue}" Background="{Binding GeneralForecast,Converter={StaticResource gf2brush}}"/> <TextBlock FontSize="16" Margin="10,0,0,0" VerticalAlignment="Bottom" Text="{Binding TemperatureLow,StringFormat='Low:\{0:N2\}'}"/> <TextBlock FontSize="16" Margin="10,0,0,0" VerticalAlignment="Bottom" Text="{Binding TemperatureHigh,StringFormat='High:\{0:N2\}'}"/> <TextBlock FontSize="16" Margin="20,0,0,0" VerticalAlignment="Bottom" Text="{Binding Percipitation,StringFormat='Percip:\{0:N2\}'}"/> </StackPanel> </Border> <DataTemplate.Triggers> <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Sunny"> <Setter Property="Background" Value="Yellow" TargetName="_border"/> </DataTrigger> <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Snowy"> <Setter Property="Background" Value="LightBlue" TargetName="_border"/> </DataTrigger> <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Rainy"> <Setter Property="Background" Value="LightGreen" TargetName="_border"/> </DataTrigger> <DataTrigger Binding="{Binding GeneralForeCastValue}" Value="Dry"> <Setter Property="Background" Value="LightYellow" TargetName="_border"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp79 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); _days.ItemsSource = Enumerable.Range(1, 30); } private void Button_Click(object sender, RoutedEventArgs e) { var data = new List<Forecast>(); int days = (int)_days.SelectedItem; var rnd = new Random(); for (int i = 0; i < days; i++) { double temp = rnd.NextDouble() * 40 - 10; var forecast = new Forecast { GeneralForeCastValue = (GeneralForecast)rnd.Next(Enum.GetValues(typeof(GeneralForecast)).Length), TemperatureLow = temp, TemperatureHigh = temp + rnd.NextDouble() * 15, Percipitation = rnd.Next(10) > 5 ? rnd.NextDouble() * 10 : 0 }; data.Add(forecast); } DataContext = data; } } public class Forecast { public GeneralForecast GeneralForeCastValue { get; set; } public double TemperatureHigh { get; set; } public double TemperatureLow { get; set; } public double Percipitation { get; set; } } public enum GeneralForecast { Sunny, Rainy, Snowy, Cloudy, Dry } }