qshinoの日記

Powershell関係と徒然なこと

wpf converter

wpf converter

動作未確認だが、Converterの実装方法を調査中。

xaml

I want to use WPF DataTrigger to check value greater than X. I know that this is only possible with IValueConverter. I have found many C# examples for that, but I need it in powershell. Could someone help me translate this to powershell?

The C# code to translate:

public class CutoffConverter : IValueConverter {
    public object ConvertTo(object obj, Type type) {
        return ((int)obj) > Cutoff;
    }

    public object ConvertFrom(object obj, Type type) {
        throw new NotImplementedException();
    }

    public int Cutoff { get; set; }
}
And the XAML

<Window.Resources>
    <myNamespace:CutoffConverter x:Key="AgeConverter"/>
</Window.Resources>

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Age,
                           Converter={StaticResource AgeConverter},
                           ConverterParameter=30}">
        <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
    </DataTrigger>
</DataTemplate.Triggers>

c

$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

参考

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

https://social.technet.microsoft.com/Forums/ie/en-US/c54f1c71-3545-4db2-aab2-9c4ce6732c06/how-to-use-ivalueconverter-from-powershell-in-xaml-gui?forum=winserverpowershell