Setting wall parameters using WPF

Code for Ironpython2 (just remove super().__init__() special syntax for Python3)

import clr    
import sys
import System
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)
#
ipy2_assembly = System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(lambda a :all(x in a.FullName for x in  ["IronPython.Modules", "2.7"]))
assembly_ipy2_Directory = System.IO.Path.GetDirectoryName( ipy2_assembly.Location )
parent_ipy2_Directory = System.IO.Directory.GetParent(assembly_ipy2_Directory).FullName
#
sys.path.append(parent_ipy2_Directory + "\\extra\\IronPython.StdLib.2.7.9")
sys.path.append(parent_ipy2_Directory + "\\bin")

clr.AddReference("IronPython.Wpf")
clr.AddReference("PresentationFramework")
clr.AddReference("System.Xml")
clr.AddReference("PresentationCore")
clr.AddReference("System.Windows")

from System.IO import StringReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application
import System.Windows.Controls 
from System.Windows.Controls import *
from System.Windows.Data import IMultiValueConverter, Binding, MultiBinding, IValueConverter
from System.Windows.Media import Brushes
import wpf

class NonNumericConverter(IMultiValueConverter):
    #
    def Convert(self, values, targetType, parameter, culture):
        text = values[0]
        out_intValue = clr.Reference[System.Int32]()
        out_decimalValue = clr.Reference[System.Decimal]()
        if System.Int32.TryParse(text, out_intValue) or System.Decimal.TryParse(text, out_decimalValue):
            return Brushes.White
        else:
            return Brushes.Tomato

    def ConvertBack(self, value, targetType, parameter, culture):
        raise NotImplementedError()
    
class MyWindow(Window):
    LAYOUT = '''
        <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window5"
            Height="300"
            Width="300">
            <Grid>
                <TextBox
                    Grid.Column="0"
                    Grid.Row="0"
                    VerticalAlignment="Top"
                    Height="20"
                    Margin="86,128,84,0"
                    x:Name="textBox" />
            </Grid>
        </Window>'''
                
    def __init__(self):
        self.ui = wpf.LoadComponent(self, StringReader(MyWindow.LAYOUT))
        # Create the binding
        binding = MultiBinding()
        converter = NonNumericConverter()
        binding.Converter = converter
        # Bind the Text property of the TextBox
        text_binding = Binding("Text")
        text_binding.Source = self.textBox
        binding.Bindings.Add(text_binding)
        # Apply the binding to the Background property
        self.textBox.SetBinding(TextBox.BackgroundProperty , binding)

objWpf = MyWindow()
objWpf.ShowDialog()

OUT = 0

for CPython3/PythonNet2 it’s a bit more complex, I’ll post the code later if you really need it

2 Likes