Looping through checkboxes on a WPF form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Dr.Gerry
Posts: 14
Last visit: Mon Jul 03, 2023 4:11 pm

Re: Looping through checkboxes on a WPF form

Post by Dr.Gerry »

Thanks JViera for your help. Once I realised the registername thing, everything fell into place. All the controls on my form, apart from the groupboxes and buttons are added at runtime, so I have to register the name each time I create a new control. But having done that, I can then reference them easily.

Thanks again
User avatar
Dr.Gerry
Posts: 14
Last visit: Mon Jul 03, 2023 4:11 pm

Re: Looping through checkboxes on a WPF form

Post by Dr.Gerry »

Thanks for your reply. I must admit I thought I had looked at all the replies to my message but I missed this one. Since then I have made a few changes to my script. Thanks to your assistance I was able to get it working, but now I have a new dilemma. I think your reply will probably answer my question, but I wanted to say 'thanks' to you first, and then explore the link you supplied. Anyway, my dilemma is this - I am trying to build the whole form at runtime. I have a WPF window with a named stackpanel. At runtime I read a CSV file ("Division","Content","Name","Path"). If Division changes I add a named GroupBox to the Stackpanel. I then add a named UniformGrid, and then add a checkbox to each UniformGrid. When the "Division" changes, I add a new GroupBox and start again. I found that when I did this, the checkboxes were not added within their relevant GroupBox/UniformGrid - they just appeared as a vertical list down the page on top of everything else.

I seem to struggle with the whole FindName process, particularly if the Name I am trying to Find has only been created at runtime. I have added a 'registername' every time I add a new item to the form, but I seemed to be unable to Find that name again.

In the end I wimped out and I have created everything but the checkboxes in the XAML and then I load each of them from a separate CSV.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Looping through checkboxes on a WPF form

Post by jvierra »

First names have to be registered in the namespace they need to be created in. You are registering all at the rot Window.

New controls have to be added to the containers content. Start with the simplest form and add a container then add a control to the container.

I recommend using a repeater to add items to a container. This can make the whole task easier.

Another issue that effects WPF forms is that they are "flowed". If you try to do too much static positioning the flow can become erratic. I suggest working with VS and the WPF examples until you learn how to manage flow.

Her is a tutorial on how to use items, containers and templates in WPF. Watch it now as it is due to be retired on 1/31.2019.
https://mva.microsoft.com/en-US/trainin ... 5604819052
User avatar
Dr.Gerry
Posts: 14
Last visit: Mon Jul 03, 2023 4:11 pm

Re: Looping through checkboxes on a WPF form

Post by Dr.Gerry »

You are worth your weight in gold! The only problem now is that I was hoping to knock this out and give it to users a week or two ago. My first thought was 'simple powershell script to map drives'........then I thought I could get clever and load stuff from files, then I thought "UI!", then I thought 'try WPF.....it will be fun they said', and now, 2 weeks later I am still learning the fundamental concepts, but fundamental concepts that weren't part of the original design brief! Oh well, I am learning heaps, and may actually have a useful Powershell tool at the end of it.

Thanks again for your help, you have really cleared up some grey areas for me.

Hmmmm......now I suppose I could create a usercontrol, and give it a data context, and bind that data to an object I create in the Powershell......Oh no, feature creep again, and worst of all, those are all just words that I have read during all this, but have no idea if they apply, or how to use them!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Looping through checkboxes on a WPF form

Post by jvierra »

It sounds to me like you are having fun. A always like the impossible because I always learn cool new things.
User avatar
Dr.Gerry
Posts: 14
Last visit: Mon Jul 03, 2023 4:11 pm

Re: Looping through checkboxes on a WPF form

Post by Dr.Gerry »

Finally! Got it working. Now my next problem. I moced up my WPF window in Visual Studio and then used the XAML in Powershell studio. Seeing as how the groupboxes don't exist until runtime I can't use any of the layout properties I used in Visual Studio, so how do I add something like this at runtime?
  1.                 <GroupBox.HeaderTemplate>
  2.                     <DataTemplate>
  3.                         <TextBlock Text="Groupbox Header Content" Foreground="#FFD78C29" Margin="0,15,0,0"/>
  4.                     </DataTemplate>
  5.                 </GroupBox.HeaderTemplate>
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Looping through checkboxes on a WPF form

Post by jvierra »

User avatar
Dr.Gerry
Posts: 14
Last visit: Mon Jul 03, 2023 4:11 pm

Re: Looping through checkboxes on a WPF form

Post by Dr.Gerry »

How do I then inject that string into my form at runtime (assuming I am understanding this!!)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Looping through checkboxes on a WPF form

Post by jvierra »

Just do the same thing in PowerShell. Convert the C# code. It shows how to create a UI object from the XAML and attach it to the parent control in your Window.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Looping through checkboxes on a WPF form

Post by jvierra »

I found an old PS XAML demo that I wrote years ago and added the fragment piece as an example:
Just execute this file at a prompt and then look at the code.
Attachments
XAML-AddFragment.ps1
(1.11 KiB) Downloaded 98 times
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked