Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Its a common requirement to convert a boolean to one of two instances of some arbitary type.</p> <p>Personally I think this is the best tool for the job: <a href="http://geekswithblogs.net/codingbloke/archive/2010/05/28/a-generic-boolean-value-converter.aspx" rel="nofollow">Generic Bool To Value Converter</a> (but there may be some bias in that opinion)</p> <p>Place an instance of the provided converter in your UserControl's resources:-</p> <pre><code> &lt;UserControl.Resources&gt; &lt;local:BoolToBrushConverter x:Key="Highlighter" FalseValue="White" TrueValue="Yellow" /&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>Now you can apply this converter to a binding the ties a boolean value from the ViewModel to a property of type <code>Brush</code>.</p> <p>However you have a bigger problem, how do you apply this converter to an individual data row? The <code>DataGrid</code> does not directly allow you set a binding that is set on each row.</p> <p>Assuming you're not a "no code in the view", you can do this with another static resource to hold a simple <code>Binding</code> then use the data grids <code>LoadingRow</code> event to apply it to each row that gets loaded.</p> <pre><code> &lt;UserControl.Resources&gt; &lt;local:BoolToBrushConverter x:Key="Highlighter" FalseValue="White" TrueValue="Yellow" /&gt; &lt;Binding x:Key="RowBackgroundBinding" Path="yourBoolProperty" Converter="{StaticResource Highlighter}" /&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>On the DataGrid:-</p> <pre><code> &lt;DataGrid x:Name="dg" LoadingRow="dg_LoadingRow" ...&gt; </code></pre> <p>Then in code-behind:-</p> <pre><code> private void dg_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.SetBinding(DataGridRow.BackgroundProperty, (Binding)Resources["RowBackgroundBinding"]); } </code></pre> <p>This will set up the binding on each row as it loads.</p>
 

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