Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few ways that you can achieve your requirements. It's a little bit inconvenient because we have to apply a <code>Style</code> with a <code>Trigger</code> on the columns of the <code>DataGrid</code> rather than the <code>DataGrid</code> itself. This means that you can't use the <code>AutoGenerateColumns</code> feature and you'll have to define them all manually as I have below. Try this:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding YourItems}" AutoGenerateColumns="False"&gt; &lt;DataGrid.Resources&gt; &lt;Style x:Key="BackgroundColourStyle" TargetType="{x:Type TextBlock}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Text" Value="1"&gt; &lt;Setter Property="Background" Value="LightGreen" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/DataGrid.Resources&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding YourPropertyName}" ElementStyle="{StaticResource BackgroundColourStyle}"&gt; &lt;/DataGridTextColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <hr> <p>UPDATE >>></p> <p>Ok, so to do this with a variable value, you'd be better off doing this the WPF data-centric way. As usual in WPF, we want to create data objects with <em>all</em> of the properties that we need to display in the UI. As such, you'd need to add a new <code>bool</code> property into whatever data type class that you are displaying in the <code>DataGrid</code>... maybe add a new column for that if you insist on using a <code>DataTable</code>.</p> <p>However, I'd advise you to use a class and if you create one, you must ensure that you implement the <code>INotifyPropertyChanged</code> interface correctly in it. You could add properties into it like this:</p> <pre><code>public int NumberValue { get; set; } // implement `INotifyPropertyChanged` here public bool HasHighlightValue { get; set; } // implement `INotifyPropertyChanged` here </code></pre> <p>Then we could use this property to highlight the relevant cell in the <code>DataGrid</code>:</p> <pre><code>&lt;Style x:Key="BackgroundColourStyle" TargetType="{x:Type TextBlock}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="HasHighlightValue" Value="True"&gt; &lt;Setter Property="Background" Value="LightGreen" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>Finally, you can set this new property in a handler or code behind in response to some user action:</p> <pre><code>// reset previous cell value YourDataType previousItem = YourItems.Where(I =&gt; i.HasHighlightValue).Single(); previousItem.HasHighlightValue = false; // set new cell value YourDataType item = YourItems.Where(I =&gt; i.NumberValue == relevantNumber).Single(); item.HasHighlightValue = true; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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