Altering DataGrid With TextBox - PowerShell
<p><strong>Background:</strong></p>

<p>I am creating a GUI using XML and PowerShell. I am using PowerShell to add functionality to the GUI. In my GUI, there are Comboboxes (drop-down meuns) as well as a textbox that filters the results of the Datagrid. I am currently working on altering the Datagrid with the results of a textbox.</p>

<p><strong>What I'm Trying To Achieve:</strong> </p>

<p>The results in the Datagrid change with the text entered in a textbox. For example, if the user enters a "W", all the results that begin with a "W" will be listed. </p>

<p><strong>The Problem:</strong></p>

<p>I am having trouble altering the contents of the Datagrid. I have tried to edit the ItemsSource. This did not work because the Datagrid did not repopulate the table when the user retracts characters from the textbox. I have also tried to edit the Items property of the Datagrid. However, the items property is not editable. I found a solution to a similar problem <a href="https://learn-powershell.net/2016/06/27/quick-hits-writing-to-a-read-only-property/" rel="nofollow">here</a>, but I couldn't utilize his solution in my current code.</p>

<p><strong>Code:</strong></p>

<pre><code>add-type @"
    public class Server
    {
        public Server() {}

        public string Column1 { get; set; }
        public string Column2 { get; set; }
        public string Column3 { get; set; }
        public string Column4 { get; set; }
        public string Column5 { get; set; }
        public string Column6 { get; set; }
    }
"@ -Language CsharpVersion3


[System.Collections.ArrayList] $Server = New-Object "System.Collections.ArrayList"

$GUI | ForEach-Object {
$ServerToAdd = new-object Server
$ServerToAdd.Column1 = $_.Results1
$ServerToAdd.Column2 = $_.Results2
$ServerToAdd.Column3 = $_.Results3
$ServerToAdd.Column4 = $_.Results4
$ServerToAdd.Column5 = $_.Results5
$ServerToAdd.Column6 = $_.Results6

$Server.add($ServerToAdd) | Out-Null 
}

$GUI.DataGrid.ItemsSource = $Server

$GUI.TextBox.Add_TextChanged({

    $InputText = $GUI.TextBox.Text 

    $GUI.DataGrid.Items | Where-Object { $_.Column1.Contains($InputText) } 

}) 
</code></pre>