Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this in XAML:</p> <pre><code>&lt;ListView x:Name="carList" VerticalAlignment="Top"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/&gt; &lt;GridViewColumn Header="Year" Width="Auto"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Year}"&gt; &lt;TextBlock.Style&gt; &lt;Style TargetType="TextBlock"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Year}" Value="2013"&gt; &lt;Setter Property="Background" Value="Blue" /&gt; &lt;Setter Property="Foreground" Value="Yellow" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TextBlock.Style&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>Result:</p> <p><img src="https://i.stack.imgur.com/0rAhG.png" alt="enter image description here"></p> <p><strong>Second solution:</strong></p> <p>You can create class for style properties.</p> <pre><code>public class CarProperty { public SolidColorBrush Background { get; set; } public SolidColorBrush Foreground { get; set; } public FontWeight FontWeight { get; set; } } </code></pre> <p>Now you can add for each property in Car class, appropriate CarProperty property:</p> <pre><code>public class Car { public Car() { YearProperty = new CarProperty { Background = new SolidColorBrush(Colors.Transparent), Foreground = new SolidColorBrush(Colors.Black), FontWeight = FontWeights.Normal }; } public string Name { get; set; } public string Make { get; set; } public string Year { get; set; } public CarProperty YearProperty { get; set; } } </code></pre> <p>generate_Click:</p> <pre><code>private void generate_Click(object sender, RoutedEventArgs e) { int i = 0; string[] name = { "Sentra", "IS", "Camry" }; string[] make = { "Nissan", "Lexus", "Toyota" }; string[] year = { "2003", "2011", "2013" }; foreach (string s in name) { Car car = new Car(); car.Name = name[i]; car.Make = make[i]; car.Year = year[i]; if (year[i] == "2013") { car.YearProperty = new CarProperty(); car.YearProperty.Background = new SolidColorBrush(Colors.Blue); car.YearProperty.Foreground = new SolidColorBrush(Colors.Yellow); car.YearProperty.FontWeight = FontWeights.Bold; } carList.Items.Add(car); i++; } } </code></pre> <p>XAML:</p> <pre><code>&lt;ListView x:Name="carList" VerticalAlignment="Top"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/&gt; &lt;GridViewColumn Header="Year" Width="Auto"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Year}"&gt; &lt;TextBlock.Style&gt; &lt;Style TargetType="TextBlock"&gt; &lt;Setter Property="Background" Value="{Binding YearProperty.Background}" /&gt; &lt;Setter Property="Foreground" Value="{Binding YearProperty.Foreground}" /&gt; &lt;Setter Property="FontWeight" Value="{Binding YearProperty.FontWeight}" /&gt; &lt;/Style&gt; &lt;/TextBlock.Style&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload