Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted (edited)

Hi all

 

I have been playing around with Powershell and the .net framework Windows Presentation Foundation (WPF). Using powershell, i can insert XAML code inside an object and execute this from within Powershell showing a GUI for users to interact with.

 

This is all working very well, and im having lost of fun playing with this (as shown by posting from home on a bank holiday monday lol), The only problem im having is when i try and edit the design of the form after the form has been generated.

 

I can change basic elements in the form (Height, Width e.c.t) by changing the desired variable and entering a new value, then passing the processing thread to the WPF generated window with a "regenerate" command. However i can not figure out how i can add extra generated content (i.e. extra text box or checkboxes) with this method, as there doesn't seem to be a method to add children / content to the canvas?

 

The only way i have found to get around this so far is to have my powershell code include a button on click even that closes the current open form, re-generates the XAML code for the window, and then show the form dialog again. This however only seems to work once, and im not sure how this would effect other options within the program.

 

Does anyone have a better method to dynamically add / remove UI elements with this?

 

Thanks

Edited by koryo
Posted

Can't you use Canvas.Children.Add(whatever_control)?

 

myCanvas.Children.Add(textBox)
myCanvas.SetLeft(textBox, xCoord)
myCanvas.SetTop(textBox, yCoord)

 

This is how I'd approach the same thing in C# at least.

Posted

Hi jamesb, thanks for your responce.

 

In responce to your help, i had a quick look at my form to try this. According to powershell there is no "add" method on the canvas children object. However i gave it a go anyway, and got a responce saying that powershell can not convert a string to the .net object that appears to be required. Looks like more fiddle pokery is required here...

 

thanks for the heads up on that hidden little method though.

Posted

 

Hi Arthur

 

Powerboots does sound very interesting, and somthing i will probably get into very soon. I was trying not to get into too many external modules for the time being as it would be nice to have cross-system code without having to distribute another module along side it.

Posted

If it's saying it can't convert, you'll probably need to strongly cast the object you're adding.

 

[TextBox]$textbox = 

 

and so forth.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...