Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set background of a datagrid cell during AutoGeneratingColumn event depending on its value?
    text
    copied!<p>I'm still fighting with manipulation of cell backgrounds so I'm asking a new question.</p> <p>A user "H.B." wrote that I can actually set the cell style during the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratingcolumn.aspx" rel="nofollow noreferrer">AutoGeneratingColumn</a> event - <a href="https://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values/5551986#comment23145839_5551986">Change DataGrid cell colour based on values</a>. The problem is that I'm not sure how to do it.</p> <p><strong>What I want:</strong> Set different <strong>background colours</strong> for each cell depending on its value. If the value is <code>null</code> I also want it <strong>not</strong> to be <strong>clickable</strong> (focusable I guess).</p> <p><strong>What I have / I'm trying to do:</strong></p> <pre><code>private void mydatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { foreach (Cell cell in e.Column) { if (cell.Value &lt; 1) { cell.Background = Color.Black; cell.isFocusable = false; } else { cell.Background = Color.Pink; } } } </code></pre> <p>This is just the pseudocode. Is something like this is possible during column auto-generation and if so, how can I edit my code so it will be valid?</p> <p>I read about value convertors but I want to know if it's somehow possible programmatically, without writing XAML.</p> <p>Please understand that I'm still a beginner to C#/WPF/DataGrid.</p> <h2>Solution part1:</h2> <p>I used the answer I accepted. Just put it into </p> <pre><code>&lt;Window.Resources&gt; &lt;local:ValueColorConverter x:Key="colorConverter"/&gt; &lt;Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}"&gt; &lt;Setter Property="Padding" Value="5"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type DataGridCell}"&gt; &lt;Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True"&gt; &lt;Border.Background&gt; &lt;MultiBinding Converter="{StaticResource colorConverter}"&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=DataGridCell}" Path="Content.Text"/&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=DataGridCell}" Path="IsSelected"/&gt; &lt;/MultiBinding&gt; &lt;/Border.Background&gt; &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; </code></pre> <p>And made for it a <code>MultiBinding</code> convertor so I can also set the background color for selected cells.</p> <h3>Problem:</h3> <p>Now I only have to solve the problem of setting focus of empty cells. Any hints?</p> <pre><code> &lt;Style.Triggers&gt; &lt;Trigger Property="HasContent" Value="False"&gt; &lt;Setter Property="Focusable" Value="False"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; </code></pre> <p>This doesn't work. I had empty strings in the empty cells, but they are filled with ´null´s so it should work, right? Or what am I doing wrong :| ?</p> <h2>Solution part 2:</h2> <p>So the code above won't work as long as the cell value is a ´TextBox´ so I decided to find another way to deal with it which can be found in my answer here: <a href="https://stackoverflow.com/a/16673602/2296407">https://stackoverflow.com/a/16673602/2296407</a></p> <p>Thanks for trying to help me :)</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