Still trying to get red negative values sorted in the datagrid...
I have tried the technique in Patrick's post here , but it has some undesirable consequences... so, I went for implementing the solution straight from the help guide... using Conditional formatting, ie, this page
It works just fine in a test WPF app I hacked... but in my real app (well, add-on actually), it does NOT work... however Snoop reports it is fine! ie Foreground color is red on a selected grid cell - but the cell displayed still has white foreground. Proof attached in images... and yes I am sure I am looking at the correct grid cell.
I just want to display values... no editing allowed, even what I show here has a border from the custom CellValuePresenter. I'd rather NOT have that also... but first issue is to sort the red negative...
The cell in question is the one SNoop has highlghted with a red border... value -5.12
PS... I am forced to use version WPF15.4 due to the addon's underlying app is provided with 15.4... I cannot change this.
THANKYOU! I have been banging my head on the wall for weeks trying to resolve this... you have solved both my issues nicely.
Extremely grateful!
Hello Trever,
Can you please try Field.Settings.EditorStyleSelector instead of Field.Settings.CellValuePresenterStyleSelector in your case?
Your code would look something like:
-----------xaml-----------xmlns:igEditors="http://infragistics.com/Editors"... <Style x:Key="CVP_RedField" TargetType="{x:Type igWPF:XamNumericEditor}"> <Setter Property="Foreground" Value="Red" /> </Style> <Style x:Key="CVP_NormalField" TargetType="{x:Type igWPF:XamNumericEditor}"> <Setter Property="Foreground" Value="Blue" /> </Style>----------------------
-----------c#----------- Style redForegroundStyle = mainpage.Resources["CVP_RedField"] as Style; Style blueForegroundStyle = mainpage.Resources["CVP_NormalField"] as Style; dg1.DefaultFieldLayout.Fields["Profit"].Settings.EditorStyleSelector = new CVPStyleSelector_Field(redForegroundStyle, blueForegroundStyle); dg1.DefaultFieldLayout.Fields["CNP"].Settings.EditorStyleSelector = new CVPStyleSelector_Field(redForegroundStyle, blueForegroundStyle);----------------------
You can use the same CVPStyleSelector_Field class.
Indeed... the XamNumericEditor below the CellValuePresenter has FG color ...white (well, it's light grey actually... but looks white ;-)... and this value is flagged as "Inherited in Snoop's Value Source. Inherited from where? The object (CVP) at higher level sets it red...(BTW... I am not expert in this stuff, forgive me if say something dumb!). If I track back up the visual tree, I eventually see DataRecordPresenter with Foreground FontControlBrush set to the white(grey) color... but that's as expected... and wanted. I do want (most) of my grid foreground white... but added this code to achieve the red negatives on just two fields
Style redForegroundStyle = mainpage.Resources["CVP_RedField"] as Style; Style blueForegroundStyle = mainpage.Resources["CVP_NormalField"] as Style; dg1.DefaultFieldLayout.Fields["Profit"].Settings.CellValuePresenterStyleSelector = new CVPStyleSelector_Field(redForegroundStyle, blueForegroundStyle); dg1.DefaultFieldLayout.Fields["CNP"].Settings.CellValuePresenterStyleSelector = new CVPStyleSelector_Field(redForegroundStyle, blueForegroundStyle);
Plus this code... taken pretty much straight out of the Help page on Conditional Formatting:
internal class CVPStyleSelector_Field : StyleSelector { private Style _redStyle; private Style _normalstyle; internal CVPStyleSelector_Field(Style redStyle, Style normalstyle) { this._redStyle = redStyle; this._normalstyle = normalstyle; } public override Style SelectStyle(object item, DependencyObject container) { bool isneg = (0 > decimal.Parse(item.ToString())); if (isneg) { return _redStyle; } else { return _normalstyle; } } }
Finally... here's my XAML code:
<Grid x:Name="mainpage" Margin="0,10,0,00" Height="Auto" VerticalAlignment="Stretch"> <Grid.Resources> <Style x:Key="CVP_RedField" TargetType="{x:Type dp:CellValuePresenter}"> <Setter Property="Foreground" Value="Red" /> </Style> <Style x:Key="CVP_NormalField" TargetType="{x:Type dp:CellValuePresenter}"> <Setter Property="Foreground" Value="Blue" /> </Style>
I kinda said already... I don't really even WANT XamNumericEditor at all... NO EDITS... I just want to display the value red if negative - period. In fact, I really WANT to get rid of the NumericEditor control... it just messes up my display when you click on it (I don't need/want that).
The other frustration for me is this: I used exactly the same code in a standalone WPF app I cut... and it works perfectly. Only difference is my real "app" is an embedded add-on in my trading platform... rather than a standalone WPF app. See next pic...
Thankyou for your help...
I suspect that your Foreground setting for CellValuePresenter is getting overridden by some other elements in the visual tree.For example, I see XamNumericEditor under CellValuePresenter in your tree.Can you please check to see if there are any elements under CellValuePresenter that have Foreground=White?