How to Add a Control to System.Windows.Controls.MenuItem Programmatically in WPF

I’m working on a new feature for Jenkins Toolset. It’s the feature that allows you to invoke a job with parameters. I need to add parameters to MenuItem according to parameter types. I have not completed the feature yet but I figured out a way to add controls programmatically, so here is the sample code.

var g = new System.Windows.Controls.Grid();
g.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
g.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

var lblText = new System.Windows.Controls.TextBlock();
lblText.Text = $"{p.Name} ";
System.Windows.Controls.Grid.SetColumn(lblText, 0);

var txtBox = new System.Windows.Controls.TextBox();
txtBox.Width = 100;
System.Windows.Controls.Grid.SetColumn(txtBox, 1);

parItem.Header = g;

Here is the sample UI.

It took me a while to figure this out, so I thought I’d share it here in my blog. 🙂

Jenkins Toolset

I used to do a lot of automation with Jenkins. I worked for a software shop that had multiple instances of Jenkins for prod, stating and development with hundreds of jobs. It was not so easy to manage all of them without a tool, so I had decided to create a desktop software with WPF. I still have the project on GitHub today.

As the context menu indicates, it can do all of those things. It can list the jobs on the specified Jenkins instance and manage them. I made it downloadable on OCI’s Object Storage from here.

Here is the view of the builds of a job.

I would like to convert this project to a MAUI project eventually. I will write about the tool more later when I have some more time.

.NET 6

I don’t really talk about .NET on this blog, but I am very interested in .NET 6. I used to be a .NET developer and I used to write Windows Form applications, WPF, Windows Service and ASP .NET applications. The reason I have been away from .NET is because the current company I work for mainly uses Linux technology. (I applied for the job because I was very interested in the Linux realm.)

I am interested in .NET 6 (as of 9/13/2021, it is in preview 7) because it has a new feature of multi platform UI. When I develop software in the last few years, I have always cared for multi platform-ness. I tried Electron for the software I developed in WPF but it didn’t click with me because it’s mainly HTML and JavaScript. I should have pursued it but the thing is that I miss C# very much.

I downloaded the .NET 6 installer for macOS and installed it. After the installation, I opened terminal and ran dotnet. Here is the output I got.

I want to make sure I am using .NET 6 preview 7. So I run dotnet --version and I got the following output.

Now I am going to create a sample code. I run dotnet new --list to list the available project type.

I’m going to create a hello world sample code in console application. I run dotnet new console helloworld to create the sample project

Once dotnet new console helloworld is successful, you get a few files like this.

The Program.cs file already has a sample code like below.

// See for more information
Console.WriteLine("Hello, World!"); 

To compile the code, you can run dotnet build in the same directory.

Once the compilation is successful, it creates bin/Debug/net6.0 directories. In net6.0 directory, it generates several compiled files like below.

As shown in the image, helloworld is an executable file, so you can be in net6.0 directory and execute it like ./helloworld But you can go back to 3 directories up and execute it like dotnet run.

According to the help, dotnet run builds and runs the project, so when you make changes to the code, you could just execute dotnet run to test things out.

I will continue to .NET 6 as time permits.