Note that there are some explanatory texts on larger screens.

plurals
  1. POForeground color not updating on a selected row in WPF DataGrid
    text
    copied!<p>I hope that some of you might have encountered the issue and can explain me what is going on here. I have tried a few fixes (within the MVVM world:) which did not help and I would like to understand the issue before applying some hack if necessary,</p> <p>I have a set of three data grid columns that use a value converter for foreground color - green for positive, red for negative.</p> <p>That works fine most of the time but sometimes (really cannot tell when and why) it does not. That can happen after around 20 minutes of running correctly with the application window not active (users look at the screen but do not interact with it).</p> <p>One important piece of information is that whenever user clicks on the row the colours get fixed immediately.</p> <p>Also it might be worth noticing that entire row colours do not update and numbers are correct....</p> <p>All the updates to the property are done with BeginInvoke on the dispatcher and might happen very often (around 20 times per second).</p> <p><img src="https://i.stack.imgur.com/9hcNI.png" alt="Wrong colour from the foreground binding"></p> <p>Any ideas welcome.</p> <p>(previously I thought it might be virtualization but this did not help)</p> <pre><code>&lt;DataGrid ItemsSource="{Binding Items}" IsReadOnly="True" AlternatingRowBackground="#E5E5E5" EnableRowVirtualization="False" VirtualizingStackPanel.IsVirtualizing="False" VirtualizingStackPanel.VirtualizationMode="Standard"&gt; </code></pre> <p><strong>Data grid column</strong></p> <pre><code>&lt;DataGridTextColumn Binding="{Binding A}" Header="A" Width="85" FontWeight="Bold"&gt; &lt;DataGridTextColumn.CellStyle&gt; &lt;Style TargetType="DataGridCell" BasedOn="{StaticResource NotSelectableCellStyle}"&gt; &lt;Setter Property="Block.TextAlignment" Value="Right"/&gt; &lt;Setter Property="Foreground" Value="{Binding A, Converter={StaticResource colorConverter}}"/&gt; &lt;/Style&gt; &lt;/DataGridTextColumn.CellStyle&gt; &lt;/DataGridTextColumn&gt; </code></pre> <p><strong>Converter</strong></p> <pre><code>public class NegativePositiveColorConverter : IValueConverter { private static readonly Brush negativeBrush; private static readonly Brush positiveBrush; static NegativePositiveColorConverter() { negativeBrush = new SolidColorBrush(Colors.Red); positiveBrush = new SolidColorBrush(Colors.Green); negativeBrush.Freeze(); positiveBrush.Freeze(); } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { decimal? decimalValue = value as decimal?; if (!decimalValue.HasValue) { return Binding.DoNothing; } if (decimalValue &lt; 0) { return negativeBrush; } return positiveBrush; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } </code></pre> <p><strong>Source object property</strong></p> <pre><code> public decimal? A { get { return this.a; } set { if (this.a!= value) { this.a= value; OnPropertyChanged("A"); } } } </code></pre> <p><strong>Cell Style (rather not important)</strong></p> <pre><code>&lt;Style TargetType="DataGridCell" x:Key="NotSelectableCellStyle" BasedOn="{StaticResource {x:Type DataGridCell}}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Background}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="Background" Value="{StaticResource EditableBrush}"/&gt; &lt;Setter Property="Padding" Value="3,0,3,0" /&gt; &lt;Setter Property="Template" Value="{StaticResource NotSelectableDataGridCellTemplate}"/&gt; &lt;/Style&gt; &lt;ControlTemplate x:Key="NotSelectableDataGridCellTemplate" TargetType="{x:Type DataGridCell}" &gt; &lt;Grid&gt; &lt;Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"&gt; &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/ControlTemplate&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