WPF. ImageSource as a var

Please help with WPF.
I try to learn more about wpf and xaml for UI in my scripts
I found really great examples on GitHub from @c.poupin . Thanks for that! so much
I tried to make a simple form with dropdown box where you can select a needed type.
and when you change selected item from list - image as well should changed for current selected item
some code below:

import clr	
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')

try:
	clr.AddReference("IronPython.Wpf")
	clr.AddReference('System.Core')
	clr.AddReference('System.Xml')
	clr.AddReference('PresentationCore')
	clr.AddReference('PresentationFramework')
except IOError:
	raise
	
from System.IO import StringReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application

from System import Uri
from System.Windows.Media.Imaging import BitmapImage

try:
	import wpf
except ImportError:
	raise
	
class DropdownInput(Window):

	LAYOUT = """
			<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"
				xmlns:local="clr-namespace:WpfApplication1"
				mc:Ignorable="d" 
				Height="500" 
				Width="320" 
				ResizeMode="NoResize"
				Title="A" 
				WindowStartupLocation="CenterScreen" 
				Topmost="True" 
				SizeToContent="Width">
    <Grid Margin="10,0,10,10">
        <Label x:Name="selection_label" Content="Select Item" HorizontalAlignment="Left" Height="30"
						VerticalAlignment="Top"/>
        <ComboBox x:Name="combo_data" HorizontalAlignment="Left" Margin="0,30,0,0"
						VerticalAlignment="Top" Width="300"/>
        <Button x:Name="button_select" Content="Select" HorizontalAlignment="Left" Height="26"
					Margin="0,63,0,0" VerticalAlignment="Bottom" Width="300"/>
        <Image x:Name="img" HorizontalAlignment="Center" Height="316" Margin="10,70,10,10" VerticalAlignment="Top" Width="260" Source="" Stretch="Fill" IsEnabled="True"/>
    </Grid>
</Window>
"""
				
	def __init__(self, title, options, description=None, uris="C://Railing/Source/2.png"):
		self.selected = None
		self.ui = wpf.LoadComponent(self, StringReader(DropdownInput.LAYOUT))
		self.ui.Title = title
	
		self.ui.selection_label.Content = description
		
		self.ui.button_select.Click += self.select_click
	
		self.ui.combo_data.Items.Clear()
		self.ui.combo_data.ItemsSource = options
		self.ui.combo_data.SelectedItem = options[-1]
		
		self.ui.img.Source = BitmapImage(Uri(uris))
	
			
	def select_click(self, sender, e):
		self.selected = self.ui.combo_data.SelectedItem
		self.DialogResult = True
		self.Close()

run = True
keys = ["A", "B", "C"]
values = [1, 2, 3]
description = "smth"

uri1 = ["C://Railing/Source/1.png", "C://Railing/Source/2.png", "C://Railing/Source/3.png"]

if values:
	if len(values) != len(keys):
		raise Exception('Length of Values and Keys must match')
	dropdown_options = {k:u for k, v, u in zip(keys, values, uri1)}
	
else:
	dropdown_options = {k:k for k in keys}
	
if run:
	form = DropdownInput('InputForm', dropdown_options.keys(), description=description, uris=dropdown_options.uri1())
	form.ShowDialog()
	if form.selected is not None:
		OUT = dropdown_options[form.selected]
else:
	OUT = 'Set RUN to True'

Hi,

here is an example with a nametuple and base64 images

you can use a DataTable instead

wpf combo box


import clr	
import sys
import System
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')

try:
	clr.AddReference("IronPython.Wpf")
	clr.AddReference('System.Core')
	clr.AddReference('System.Xml')
	clr.AddReference('PresentationCore')
	clr.AddReference('PresentationFramework')
except IOError:
	raise
	
from System.IO import StringReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application

from System import Uri
from System.Windows.Media.Imaging import BitmapImage

try:
	import wpf
except ImportError:
	raise
	
from collections import namedtuple
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows")
import System.Drawing
from System.Drawing import *
import System.Windows.Media
import traceback
	
class DropdownInput(Window):

	LAYOUT = '''
			<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"
				xmlns:local="clr-namespace:WpfApplication1"
				mc:Ignorable="d" 
				Height="300" 
				Width="300" 
				ResizeMode="NoResize"
				Title="A" 
				WindowStartupLocation="CenterScreen" 
				Topmost="True" 
				SizeToContent="Width">
				<Grid Margin="10,0,10,10">
					<Label x:Name="selection_label" Content="Select Item" HorizontalAlignment="Left" Height="30"
						VerticalAlignment="Top"/>
						<ComboBox x:Name="combo_data" HorizontalAlignment="Left" Margin="0,30,0,0" VerticalAlignment="Top" Width="300" DisplayMemberPath="Key" SelectionChanged="Combox_Changed"/>
						<Button x:Name="button_select" Content="Select" HorizontalAlignment="Left" Height="26" Margin="0,63,0,0" VerticalAlignment="Bottom" Width="300" Click="ButtonClick"/>
					<Image x:Name="img" HorizontalAlignment="Center" Height="80" Margin="10,70,10,10" VerticalAlignment="Top" Width="80" Source="" Stretch="Fill" IsEnabled="True"/>
				</Grid>
			</Window>'''
				
	def __init__(self, title, options, description=None):
		self.selected = None
		self.ui = wpf.LoadComponent(self, StringReader(DropdownInput.LAYOUT))
		self.ui.Title = title
		self.error = None
		#
		self.ui.selection_label.Content = description
		#
		self.ui.combo_data.Items.Clear()
		self.ui.combo_data.ItemsSource = options
	
			
	def ButtonClick(self, sender, e):
		self.selected = self.ui.combo_data.SelectedItem
		self.DialogResult = True
		self.Close()
		
	def Combox_Changed(self, sender, e):
		self.selected = self.ui.combo_data.SelectedItem
		try:
			binaryData  = System.Convert.FromBase64String(self.selected.Base64Img)
			bi = System.Windows.Media.Imaging.BitmapImage()
			bi.BeginInit()
			bi.StreamSource = System.IO.MemoryStream(binaryData)
			bi.EndInit()
			#
			self.ui.img.Source = bi
		except Exception as ex:
			self.error = traceback.format_exc()


keys = IN[0] # ["Google", "Autodesk", "Twitter"]
values = IN[1] # ["https://www.google.com/","https://www.autodesk.fr/", "https://twitter.com/"]
lst_base64Img = IN[2] # ["iVBORw....", "iVBORw0K....", "iVBORw0..."]
description = "Choose a Site"

MyImage = namedtuple('MyImage', ['Key', 'Value', 'Base64Img'])
data = []
for key_, value_, base64String in zip(keys, values, lst_base64Img):
	data.append(MyImage(key_, value_, base64String))

form = DropdownInput('InputForm', data, description=description)
form.ShowDialog()
if form.selected is not None:
	OUT = form.selected.Value
5 Likes

Thanks! You are the best!

1 Like