Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have grid and i want to color foreground red if Qty < 0 .
I am not allow to add code at design time in xaml So i run time add
C#
Binding binding = new Binding("Qty") { Converter = new RedConverter() };
Style st = new Style(typeof(GridColumn));
st.Setters.Add(new Setter(Control.ForegroundProperty, binding));
_Left_Part_DE.Columns["Qty"].Style = st;


and my converter class is

C#
public class RedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return value.Convert_To_Number_DE<decimal>() < 0 ? Brushes.Red : Brushes.Black;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }



problem is that it is not occurring any error nor invoking the Convert method
what is lacking
Posted
Updated 1-Aug-13 1:57am
v4

you set binding in the style setter for column. But according your question, you need it for individual cell, not for column. What is your _Left_Part_DE? If it has property like CellStyle, try to use it instead of Style (in such case Style target type should be changed as well).
C#
Binding binding = new Binding("Qty") { Converter = new RedConverter() };
Style st = new Style(typeof(CellContentPresenter));
st.Setters.Add(new Setter(Control.ForegroundProperty, binding));
_Left_Part_DE.Columns["Qty"].CellStyle = st;
 
Share this answer
 
v2
Comments
kiranrahir 2-Aug-13 8:15am    
_Left_Part_DE is my DevExpress.XtraGrid grid and "Qty" is column.
I want to use Style.
if Qty < 0 than cell forecolor should be red. Can i do it with Style - IValueConverter?
Irina Pykhova 2-Aug-13 8:31am    
in your case Style - is a column style. It is applied to the column as a single FrameworkElement, not to individual cells. Use CellStyle property. Here is DevExpress sample of cell appearance customization: http://documentation.devexpress.com/#WPF/CustomDocument6772. They use CellStyle property. In the sample they set it in xaml, you should do the same from code.
kiranrahir 3-Aug-13 1:05am    
I want exactly that. I had read that artical, but problem is that my itemsouce is not fixed so
cant add this code

<dxg:gridcontrol.resources xmlns:dxg="#unknown">
<Style x:Key="customCellStyle"
BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"
TargetType="dxg:CellContentPresenter">
<setter property="Background" value="{Binding Path=RowData.Row.UnitsOnOrder, Converter={local:IntToColorConverter}}"></setter>
</Style>
</dxg:gridcontrol.resources>

i want its behind c# code. How can i assign Style to cellstyle
Irina Pykhova 3-Aug-13 5:09am    
Just rename style target type and property in your code.
I added code snippet to my answer above
And it doesn't make any sense whether your items source is fixed or no. This should work in any case both from xaml and from code
kiranrahir 3-Aug-13 6:05am    
May you think that i m stupid but i am not seeing any Code Snippet.
in my code i add
xaml part

<dc:GridControl Name="Left_Part_DE" >
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor Name="ctlDataControlDetailDescriptor" >
<dxg:DataControlDetailDescriptor.DataControl>
<dc:GridControl AutoPopulateColumns="True" Name="ctl_Child_Grid">
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" AutoWidth="True" NavigationStyle="Row" />
</dxg:GridControl.View>
</dc:GridControl >
</dxg:DataControlDetailDescriptor.DataControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False" AutoWidth="True" NavigationStyle="Row" />
</dxg:GridControl.View>
</dc:GridControl>

c# part


Style _Style = new Style(typeof(CellContentPresenter));
Setter _Setter = new Setter();
_Setter.Property = CellContentPresenter.BackgroundProperty;
_Setter.Value = new Binding("RowData.Row.Qty") { Converter = new RedConverter() };
_Style.Setters.Add(_Setter);
_Left_Part_DE.Columns["Qty"].CellStyle = Style;


but problem is that no error occur nor event is invoking for background.
Am i missing any thing?
XML
<dx:gridcontrol name="ctl_Grid" height="900" width="900" xmlns:dx="#unknown">
            <dxg:gridcontrol.detaildescriptor xmlns:dxg="#unknown">
                <dxg:datacontroldetaildescriptor name="ctlDataControlDetailDescriptor">
                    <dxg:datacontroldetaildescriptor.datacontrol>
                        <dx:gridcontrol autopopulatecolumns="True" name="ctl_Child_Grid">
                            <dxg:gridcontrol.view>
                                <dxg:tableview showgrouppanel="False" autowidth="True" navigationstyle="Row" />
                            </dxg:gridcontrol.view>
                        </dx:gridcontrol>
                    </dxg:datacontroldetaildescriptor.datacontrol>
                </dxg:datacontroldetaildescriptor>
            </dxg:gridcontrol.detaildescriptor>
            <dxg:gridcontrol.view xmlns:dxg="#unknown">
                <dxg:tableview showgrouppanel="False" autowidth="True" navigationstyle="Row" />
            </dxg:gridcontrol.view>
        </dx:gridcontrol>


c# part

C#
Style _Style = new Style(typeof(CellContentPresenter)); 
Setter _Setter = new Setter(); _
Setter.Property = CellContentPresenter.BackgroundProperty; 
_Setter.Value = new Binding("RowData.Row.Qty") { Converter = new RedConverter() }; 
_Style.Setters.Add(_Setter); _
Left_Part_DE.Columns["Qty"].CellStyle = Style;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900