Home > Mobile >  Powershell WPF handle single row colors
Powershell WPF handle single row colors

Time:05-01

I'm trying to adapt a script of mine who just shows a simple WPF datagrid.

It has 4 Columns and i need to set a color on a few rows if its content respects a condition (For instance, the cell to check fetches a datetime in this format: 'dd/mm/aaaa hh:mm:ss')

This is how add my rows like this:

$myDatagrid.AddChild([pscustomobject]@{Column0=$Value0;Column1=$Value1;Column2=$Value2;Column3=$Value3})

Now before adding it one of the values gets checked, and i want to set a colour if the condition is satisfacted.

Back when i was using it under winforms i'd just do it like this:

$myDatagrid.Rows[$rownumber].DefaultCellStyle.BackColor = "#DCDCDC"

Is there anything similar in WPF allowing me to pick a line and set its colour?

############### What i've tried: ######################

I've read about one could intercept the cell creation event and i could set the colour inside it, result was just a big headache trying to undestand it as all i found was in C#. It's a small script spinning up a form and not much more i don't really want to study a whole new language for it.


I tryed bindings:

<Style TargetType="DataGridRow"> 
  <Style.Triggers>
    <DataTrigger Binding="{Binding Column1}" Value="True">
      <Setter Property="Background" Value="White"></Setter>
    </DataTrigger>
    <DataTrigger Binding="{Binding Column1}" Value="False">
      <Setter Property="Background" Value="#8e8f94"></Setter>
    </DataTrigger>
  </Style.Triggers>
</Style>

I did test this on a cell where i store a true/false value but i have no clue how to do it on a cell that contains a date 'dd/mm/aaaa hh:mm:ss' Now if i could set the color in the code it would be simple as before filling the cell i have a DateTime and that's easyer to deal with.


I did spend a lot of time browsing the microsoft docs on it and looking for other answers before posting, i actually found very little info scattered in dozen of articles and documents. Is there a better way to scrape documentation or everyone else is having the same issue?


Edit: i'm reading about DataGridRow, how do i get datagridrow from datagrid?

CodePudding user response:

Okay... took me long enough but i did it. I know, it's ugly as hell, but it works. If anyone is interested you have something to start with.

$myDatagridRowArray= @() #Creating a rows array
foreach($row in $dataset.tables[0].Rows) #Cycle rows from a dataset table
{

$myDatagridRowItem = new-object myRowClass

$myDatagridRow = New-Object System.Windows.Controls.DataGridRow
$myDatagridRow.item = $myDatagridRowItem

$myDatagridRowItem.Column1 = $Row[1]
$myDatagridRowItem.Column2 = $Row[2]
$myDatagridRowItem.Column3 = $Row[3]
$myDatagridRowItem.Column4 = $Row[4]

if ( some conditionsomething )
    {
        $myDatagridRow.background = '#8e8fff'
    }


$myDatagridRowArray  = $myDatagridRow

  }
  $i = 0
 foreach ($row in $myDatagridRowArray){
$Datagrid.AddChild($myDatagridRowArray[$i])
$i  
}

i only had an issue with it beeing the sorting nomore works, now working on it... (neither this line nor actually manually sorting the grid)

$Datagrid.Items.SortDescriptions.Add([System.ComponentModel.SortDescription]@{PropertyName="Column1"; Direction="Ascending"})
  • Related