Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
180
How to add CellValuePresenter to xamDataGrid in code
posted

Hi,

 

I have the following question : how do I add a cellvalue presenter style to my datagrid through the c sharp code instead of the XAML so I can modify this according to the user's preferences ?

The following style is included in my XAML :

                                <Style TargetType="{x:Type igDP:CellValuePresenter}">
                                    <Setter Property="TextBlock.Background" Value="{StaticResource ControlBackgroundBrush}" />
                                    <Setter Property="TextBlock.Foreground" Value="white" />
                                    <Setter Property="BackgroundSelected" Value="{StaticResource FPSGreenBGBrush}" />
                                </Style>

 

I would like to add the cell value presenter to my xamdatagrid rather by c# code, below you can find what I have so far. How to add this cellvaluepresenter style now to "xamMainGrid" ?

                //New Brush : light green
                System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb((byte)
                0xD5, (byte)0xE3, (byte)0xD9);

                // Create the brush.
                System.Windows.Media.SolidColorBrush brushlightgreen = new
                System.Windows.Media.SolidColorBrush(color);

                //New Brush : black
                System.Windows.Media.Color blackcolor = System.Windows.Media.Color.FromRgb((byte)
                0x0A, (byte)0x0A, (byte)0x0A);

                // Create the brush.
                System.Windows.Media.SolidColorBrush brushblack = new
                System.Windows.Media.SolidColorBrush(blackcolor);


                ResourceDictionary myDataGridResource = new ResourceDictionary();

                //New CellValuePresenter
                CellValuePresenter DataGridLayout = new CellValuePresenter();
                DataGridLayout.Background = brushlightgreen;
                DataGridLayout.Foreground = brushblack;

                //this.xamMainGrid.Resources

 

Thank you in advance

Tim

 

  • 138253
    Offline posted

    Hello Tim,

     

    It has been a while since you have made your post, in case you still need support I will be glad to assist you further. I suppose the other community members can benefit from this answer as well. I have been looking into your post and the code you have posted and I suggest you create a Style for the CellValuePresenter and set it to the XamDataGrid’s FieldSettigs’ CellValuePresenterStyle Property like this:

     

    Style MyStyle = new Style(typeof(CellValuePresenter));
    MyStyle.Setters.Add(new Setter(CellValuePresenter.BackgroundProperty, brushlightgreen));
    MyStyle.Setters.Add(new Setter(CellValuePresenter.ForegroundProperty, brushblack));
    xamDataGrid1.FieldSettings.CellValuePresenterStyle = MyStyle;
    

     

    Feel free to write me if you have further questions.