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
30
Changing the grid selector background colour works in XAML, but not in code behind
posted

Hi, here's how I do it in XAML:

<Style TargetType="{x:Type igWPF:CellValuePresenter}" x:Key="colorCell">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Background" Value="SkyBlue"/>
</DataTrigger>
</Style.Triggers>
</Style>

However, I need to be ale to change the selector's colour dynamically. In the debugger, the trigger's data seems to be correctly set, but I don't see the effect in the grid.

DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding("{Binding IsSelected}");
dataTrigger.Value = "True";
dataTrigger.Setters.Add(new Setter(CellValuePresenter.BackgroundProperty, Brushes.Red));
cellStyle.Triggers.Add(dataTrigger);

Please let me know if I need to change my code to make it work or refresh the grid somehow.

Thank you,

Paul

Parents
No Data
Reply
  • 2680
    Verified Answer
    Offline posted

    Hi Paul,

    Thank you for posting to Infragistics Community!

    Before I give suggestions about programmatically adding a Style, targeting an element within the XamDataGrid, I would like to discuss the XAML style snippet you have shared. I am wondering what is the result of this style in your app, does it really affect the cell value presenters selected color, even when defined in the template? I am asking this, as testing it on my side, this style does not seem to achieve anything. Also, the title of this thread mentions styling the Record Selector, however, the provided style is not related to it, at least base on my understanding so far.

    Can you please, specify what is the purpose of the style, namely wether it is to:

    • Set the background of the row selector element – that is the small rectangle area at the start of each record containing a triangular selector icon?
    • Set the background of the selected cell? The precondition here would be to have allowed Cell Selection by setting the SelectionTypeCell property of the FieldLayoutSettings to a value different than ‘None’, as well as the FieldSettings’ CellClickAction to ‘SelectCell’.
    • Set the background of certain cells of a selected record?

    To illustrate all this, I am attaching a sample app demonstrating styles that achieve the scenarios of the selected cells and record. As you will see, the sample applies Red background for individual selected cells, Yellow background for a selected record, and an Orange background for cells of a selected record having field names either “Region” or “Coal”.

    Please, take a look at it and let me know if they accurately demonstrate what you are trying to achieve. If that is the case, there should be no issue in creating the same styles programmatically, with the approach you have already attempted, only targeting the needed properties in this case. Just make sure not to forget adding the style to the grid’s Resources collection as well.

    In case the sample does not really reflect your requirements, please, feel free to share more details, as well as modify it with the relevant code, so that I can give further suggestions. Thank you for your cooperation.

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer

    4428.XDGProgrammaticStyle.zip

Children