Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Datagrid not updating color until I click the header or scroll down.

$
0
0

Hi guys,

Looking for some help, this has been getting the better of me for a while and hoping you can help.

Here is an example script showing the issue

#Load required libraries
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing

[xml]$xaml = @"<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls" Title="Example"  WindowState="Maximized"><Grid><DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Height="227" Margin="71,40,0,0" VerticalAlignment="Top" Width="598" AutoGenerateColumns="False" ><DataGrid.CellStyle><Style TargetType="{x:Type DataGridCell}"><Setter Property="HorizontalContentAlignment" Value="Center" /><Style.Triggers><DataTrigger Binding="{Binding Monitoring}" Value="True"><Setter Property="Background" Value="LightBlue" /></DataTrigger></Style.Triggers></Style></DataGrid.CellStyle><DataGrid.Columns><DataGridTemplateColumn><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBox IsChecked="{Binding Path=Monitoring, UpdateSourceTrigger=PropertyChanged}" /></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn Header="Process Name" Binding="{Binding ProcessName}" Width="*" CanUserResize="False" CanUserSort="True" /><DataGridTextColumn Header="PID" Binding="{Binding ID}" Width="*" CanUserResize="False" CanUserSort="True" /><DataGridTextColumn Header="Start Time" Binding="{Binding StartTime}" Width="*" CanUserResize="False" CanUserSort="True" /></DataGrid.Columns></DataGrid><Button x:Name="btn_start" Content="Example" HorizontalAlignment="Left" Width="112" Height="32" Margin="71,284,0,103" /></Grid></Window>"@

#Read the form

$Reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Form = [Windows.Markup.XamlReader]::Load($reader)

#AutoFind all controls
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object {
    New-Variable  -Name $_.Name -Value $Form.FindName($_.Name) -Force
}

$btn_start.Add_Click( {
        #$Processes = get-process

        $data = [System.Collections.ObjectModel.ObservableCollection[PSObject]](get-process)
        $data | ForEach-Object {
            $_ | Add-Member -MemberType NoteProperty -Name Monitoring -Value $False -TypeName System.Boolean; }


        $dataGrid.ItemsSource = $data
    })

#Mandetory last line of every script to load form
[void]$Form.ShowDialog()
Basically I want each row to become a different color if the checkbox is enabled, it works but only once i click the header or scroll down alot.


Richard Knight | Collection Refresh Manager |Automate detection rules for patch \ msp files |Twitter


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>