How to use IValueConverter from PowerShell (in XAML GUI)

I was trying to follow following post related to using IValueConverter in PowerShell and WPF XAML GUI.

https://stackoverflow.com/questions/14281671/how-to-use-ivalueconverter-from-poweshell/31741953#31741953

I am using PowerShell 4.0.

Can anyone help by providing a small example powershell script that uses IValueConverter (say, to convert if value is >50 then True else False)

July 31st, 2015 5:28am

Hi UC,

sure thing, here's an example:

$src = @'
using System;
using System.Globalization;
using System.Windows.Data;

namespace MyNamespace
{
    public class CutoffConverter : IValueConverter
    {
        public int Cutoff { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((int)value) > Cutoff;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
'@

Add-Type -AssemblyName PresentationFramework    
Add-Type -TypeDefinition $src -ReferencedAssemblies PresentationFramework

$c = New-Object MyNamespace.CutoffConverter
$c.Cutoff = 50
$c.Convert(45, $null, $null, $null)
$c.Convert(55, $null, $null, $null)

Cheers,

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 6:26am

Hi Fred,

Thanks for your help and providing a script to use IValueConverter. It works pretty well with fixing a typo:

$c = new MyNamespace.CutoffConverter
#replace with:
$c = new-object MyNamespace.CutoffConverter

my apologies if I was not clear in my question.. I am looking for an example to use it in an XAML GUI. so, along with your code, I have following XAML code to show a GUI and here is what I am looking to achive with IValueConverter:
If I enter a number in textbox that is say, less than cutoff (e.g. 50) then TextBox background should be red. Similarly, if the value is greater than 50 then it should set textbox background to Green.

Here is my XAML code.. I just don't know how to use converter in XAML

$xaml = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Test IValueConverter" Height="143.58" Width="300">
    <Grid>
        <Label x:Name="lblHeader" Content="Enter a numeric value" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.479,0.457" Width="132"/>
        <TextBox x:Name="txtTestValue" HorizontalAlignment="Left" Height="23" Margin="147,13,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="82"/>

    </Grid>
</Window>
"@


TIA.

July 31st, 2015 7:08am

Why so much code?

PS C:\scripts> if( ($x=[convert]::ChangeType(40,[int32])) -lt 50 ){$x}
40
PS C:\scripts> if( ($x=[convert]::ChangeType(50,[int32])) -lt 50 ){$x}
PS C:\scripts>

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 8:06am

my purpose of using IValueConverter is to format controls in WPF gui based on value of a control (say, a textbox).
July 31st, 2015 8:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics