• 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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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

    • No registered users viewing this page.