How to change selection colors

I use a SfTreeGrid with CellTemplate selectors for textboxes and labels. I can't figure out how to change the background and foreground color of selected 
row (which has textboxes and labels in it). I want the foreground color of a textboxes and labels into selected row to be white, to make them easier to read (now they stay black). Can you give me a guidline how to do that?

3 Replies

DY Deivaselvan Y Syncfusion Team October 3, 2018 03:51 AM UTC

Hi Iavilo, 

Thank you for contacting Syncfusion support.

 
We have analyzed your query “To change background and foreground of selected rows when using CellTemplateSelectors”. By default, we can achieve this using SelectionBackground and SelectionForeground property in SfTreeGrid. But for template column the background and foreground will not be applied since SelectionBackground and SelectionForeground will be applied to RowControl. You achieve this for template column by changing the TemplateCoumn elements background and foreground using SelectionChanged event as like below code snippet 

private void treeGrid_SelectionChanged(object sender, GridSelectionChangedEventArgs e) 
{ 
     var treeGrid = sender as SfTreeGrid; 
     TreeGridRowInfo removedRowInfo = null; 
     TreeGridRowInfo addedRowInfo = null; 
     if (e.AddedItems.Count > 0) 
        addedRowInfo = e.AddedItems[0] as TreeGridRowInfo; 
     if (e.RemovedItems.Count > 0) 
        removedRowInfo = e.RemovedItems[0] as TreeGridRowInfo; 
            treeGrid.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => 
{ 
     TreeDataRowBase removedRow = null; 
     TreeDataRowBase addedRow = null; 
     if (addedRowInfo != null) 
        addedRow = treeGrid.GetTreeGridRowGenerator().Items.FirstOrDefault(r => r.RowIndex == addedRowInfo.RowIndex); 
     if (removedRowInfo != null) 
        removedRow = treeGrid.GetTreeGridRowGenerator().Items.FirstOrDefault(row => row.RowIndex == removedRowInfo.RowIndex); 
     if (addedRow != null) 
     { 
          var addedrowControl = addedRow.Element as TreeGridRowControl; 
          var textBox = GridUtil.FindDescendantChildByType(addedrowControl, typeof(TextBox)) as TextBox; 
          if (textBox != null) 
              textBox.Foreground = new SolidColorBrush(Colors.White); 
          else 
          { 
              var label = GridUtil.FindDescendantChildByType(addedrowControl, typeof(Label)) as Label; 
              if (label != null) 
                 label.Foreground = new SolidColorBrush(Colors.White); 
          } 
     } 
     if (removedRow != null) 
     { 
           var removedRowControl = removedRow.Element as TreeGridRowControl; 
           var textBox = GridUtil.FindDescendantChildByType(removedRowControl, typeof(TextBox)) as TextBox; 
           if (textBox != null) 
              textBox.Foreground = new SolidColorBrush(Colors.Black); 
           else 
           { 
               var label = GridUtil.FindDescendantChildByType(removedRowControl, typeof(Label)) as Label; 
               if (label != null) 
                   label.Foreground = new SolidColorBrush(Colors.Black); 
           } 
     } 
  })); 
} 

Please find sample for the same from the below link and let us know is this helps you 
Sample link : 

Regards, 
Deivaselvan 



IJ Ivailo J October 4, 2018 12:07 PM UTC

Thanks for the help! Although I had to fix some things, it works and gave me what I needed.



DY Deivaselvan Y Syncfusion Team October 5, 2018 06:49 AM UTC

Hi Ivalio,

We are happy to hear that the given solution resolves your requirement. Please let us know if you require any further assistance.

Regards,
Deivaselvan 


Loader.
Up arrow icon