-1

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

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
naschd
  • 31
  • 1
  • 9
  • [How to programmatically select and focus a row or cell in a DataGrid in WPF](https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/) – mm8 Jun 04 '19 at 13:40
  • please add your code so it is clearer what your problem is What could probably be done is use a DataTrigger bound to IsSelected and if it is true then set the background colour to your desired highlighted colour – yawnobleix Jun 04 '19 at 14:03
  • @yawnobleix Could you please post the some code if it is possible to do it just in C# ( not in xaml) ? Thanks – naschd Jun 11 '19 at 07:10
  • please see my update, it is possible to create a binding in the code – yawnobleix Jun 11 '19 at 08:10

1 Answers1

1

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

    <Style TargetType="{x:Type ContentControl}">
        <Setter Property="Visibility" Value="Collapsed"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

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.

        gridLine.SetBinding(BackgroundProperty, new Binding(nameof(IsSelected)) { Source = ListBoxItem, Converter = new SelectedToBackgroundConverter() });

Doing this in code though is much more complicated than using xaml.

yawnobleix
  • 1,204
  • 10
  • 21
  • well there are some things I do not understand ... above which entity I do "SetBinding"... what is grid line ? DataGrid has no property gridLine also **"IsSelected"** is for me unknown , coud you please describe it more ? – naschd Jun 18 '19 at 06:42