• 0

[C# WPF] Dynamically adding items to content


Question

Hello gang,

I am playing around with WPF and I cannot find out how to dynamically add multiple items to the Content. I need to have to TextBlocks and two images on a UserControl.

"MenuCount" and "MenuText" are two TextBlocks that are declared within the class and can be modified via properties.

The code I have so far is:


public MainPage()
: base()
{
InitializeComponent();
DataContext = this;

MenuText.Text = "Menu Text";
MenuText.FontSize = 20;
MenuText.FontFamily = new System.Windows.Media.FontFamily("Courier");
MenuText.Margin = new Thickness(300, 0, 0, 0);
MenuText.Foreground = this.FontColor;

MenuCount.Text = "1 of 100";
MenuCount.FontSize = 20;
MenuCount.FontFamily = new System.Windows.Media.FontFamily("Courier");
MenuCount.Margin = new Thickness(500, 0, 0, 0);
MenuCount.Foreground = this.FontColor;

MainContent = MenuText;
MainContent = MenuCount; //This is where the code is incorrect, obviously. I need to have both of these items, and two images (not in the code yet) within this.
}
[/CODE]

[CODE]
<UserControl x:Class="BediaMenu.MainPage"
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" mc:Ignorable="d" d:DesignHeight="55" d:DesignWidth="458">
<StackPanel HorizontalAlignment="Left" Margin="10">
<ContentControl Content="{Binding MainContent}"/>
</StackPanel>
</UserControl>
[/CODE]

Any thoughts?

4 answers to this question

Recommended Posts

  • 0

Can't you just use ContentControl.AddChild?

However, it's considered a bad idea define all markup and styles in C# code. You should make use of the true power of WPF which is its binding and template framework. Since you're only creating static content, you should be fine using a ListBox with a custom ItemTemplate and bind it to the collection to display. That way, you have a beautifully structured MVVM architecture. :)

Check out the MSDN page on ItemTemplate for a good example. You can then further style those ListBoxItems with an ItemContainerStyle.

  • 0

why code stuff in c# and not use the XAML markup?

Besides, those big margins you use.. I think you will have some big problems later when you have more stuff in your window

I have worked with VB for 18 years and C# was the logical next move. I started playing with WPF to get a handle on it's tech and I was testing some of it via code, as opposed to XAML, just to see the process.

I have changed this code to XAML using TextBlocks to display the data., but thanks for the heads up.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The constant interface inconsistencies have nothing to do with anything other than their own negligence and refusal to fix it. They are lazy plain and simple. These UI inconsistencies have been there for years and not one tech at Microsoft has taken the time to fix it. It's unacceptable at this point. They keep overloading the OS with ads and garbage nobody asked for but don't fix the issues they have.
    • Surgie matched da Vinci precision in live trials; Unitree’s DoD listing complicates hospital adoption Teleoperated humanoid robots completed live in-vivo surgery for the first time in medical history, UC San Diego researchers reported in Nature on July 8, 2026 — a proof of concept that could eventually extend surgical care to rural communities, battlefields, and remote settings where surgical specialists are absent. But the specific robot at the center of the breakthrough, the Unitree G1, was added to the U.S. Department of Defense's list of Chinese military companies just 30 days before the paper was published, carries a documented wormable security exploit affecting all units ever manufactured, and continuously transmits sensor data to servers in China without notifying the operator. The achievement is real, and the complications are real. Institutions evaluating this technology need to understand both. https://www.techtimes.com/articles/320157/20260711/humanoid-robots-perform-first-live-surgery-pentagon-flags-hardware-chinese-military-tech.htm  
    • Failed to open \EFI\ubuntu\ - Not found That message keeps popping up every time I boot the computer. Stays there for a few seconds than vanishes and the PC loads Linux Mint, but its really annoying the hell out of me, so, does anyone here know and can tell me how to fix that or get rid of it please? Thank you  
    • It's in fashion to bash Microsoft for even existing. It's getting old fast. People talk like other vendors' products are flawless and without issues.
  • Recent Achievements

    • Rising Star
      ExPat went up a rank
      Rising Star
    • Reacting Well
      Gideon Waxfarb earned a badge
      Reacting Well
    • First Post
      NovaEdgeX earned a badge
      First Post
    • One Month Later
      pahariyaseo earned a badge
      One Month Later
    • Week One Done
      pahariyaseo earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      431
    2. 2
      PsYcHoKiLLa
      146
    3. 3
      Nick H.
      89
    4. 4
      Steven P.
      88
    5. 5
      +Edouard
      79
  • Tell a friend

    Love Neowin? Tell a friend!