Set a named item value in a XAML with PowerShell

Hello,

I wrote this small function to set the value depending on the control type.

Function Set-ControlValue {
    [CmdletBinding()]
    Param(
        [String[]]$Control,
        [String]$Value,
        [Object]$Window = $Window
    )
    foreach ($control in $control) {
        switch ($Window.$Control.GetType().Name) {
            'TextBox' {
                $Window.$Control.Text = $Value
            }
            'TextBlock' {
                $Window.$Control.Text = $Value
            }
            'ComboBox' {
                $Window.$Control.SelectedItem = $Value
            }
            'Button' {
                $Window.$Control.Content = $Value
            }
        }
    }
}

In the future I will add more control types in the switch for the function to take more cases in her scope.

Leave a Reply

Your email address will not be published. Required fields are marked *