This is as respond to code mentioned here which partly works for me but not as desired and unfortunately in WPF I am not able to cope with this problem and find solution. Because I was not able to find functional solution I am asking this question.
Get selected row item in DataGrid WPF
Was tried to achieve the result
Respond
answered Apr 19 '11 at 22:01 by Bahaa Salaheldin
was most useful
But what happens is that while I am scrolling down , selected line is repeatedly showing up , I think because of "ContainerFromItem" approach. Is it possible to change background of DataGrid programmatically ? - only in C# code ? What I tried was that I played a little with ... selected index and so and it is not as easy as it in WindowsForms is . So I tried to find out how to perform Highlighting . What I am using are : DataTable in DataObject that is binded DataGrid Items Soruce is dataObject.DataTable I know that I have to perform .Background = Brushes."DesiredColor" on DataGridRow But I am not sure if there is any relation between DataGrid Selected Index / Selected Item / SelectedItems and Background property Thank you.
11/06/19 More info added:
Here I am posting example code , but this code is functional for windows forms , I thought I can do something like this.
foreach (DataGridViewRow row in dgvNetlist.Rows)
{
if (row.Cells[2].Value.ToString().Contains(Messages.SingleConnection))
row.DefaultCellStyle.BackColor = databaseColor[0]; //error color
else if (row.Cells[2].Value.ToString().Contains(Messages.MissingTP))
row.DefaultCellStyle.BackColor = databaseColor[1];
else if (row.Cells[2].Value.ToString().Contains(Messages.MissingConnection))//if message cell contains missing connections
row.DefaultCellStyle.BackColor = databaseColor[2];
else if (row.Cells[2].Value.ToString().Contains(Messages.MultipleTPs) && cbHideMultipleTPs.Checked == false) //if message cell contains multiple TPs
row.DefaultCellStyle.BackColor = databaseColor[3];
else if (row.Cells[2].Value.ToString().Contains(Messages.EmptyNet)) //if message cell contains Empty net
row.DefaultCellStyle.BackColor = databaseColor[0];
else
row.DefaultCellStyle.BackColor = databaseColor[4]; //OK color
if (row.Cells[4].Value.ToString().Equals("True")) //if row is marked -> marked color
row.DefaultCellStyle.BackColor = databaseColor[5];
i++;
}
But with own rules , for example , user has to "save" some row, he wants to highlight the row. What I want to do is in steps:
1) Get the item ID from selected ROW ... Item ID I mean the original ID in table for instance ID number 950
2) do some highlight action , eg. background to different colour.
3) save the ID to the user settings because each user can have different rows highlighted
4) on another application start , use some cycle to find saved rows ( does not matter if there are more or less items in database ) and highlight the rows - does not matter how ordered they are - highlighting depends on item ID ...
I thought it is somehow possible while I can get ID from datagrid-> selected item
but I found no way to do it like in code posted above.
Hope this will make it more clear
Since you don't have any code posted its difficult to tell what exactly you want. To change the background colour use a DataTrigger bound to IsSelected and if it is true then set the background colour to your desired highlighted colour
UPDATE: If you want to do it programatically then you can create the binding in the code, where Listboxitem is the listbox containing the item and SelectedToBackgroundConverter is a converter which implements the rules you mentioned in your post.
Doing this in code though is much more complicated than using xaml.