Kioko Kiaza
Kioko Kiaza

Reputation: 1398

Create a listbox from code and style list items

I have this code to create a listbox on my Menu.xaml code behind (Menu.xaml.vb):

Dim mm as new ListBox()
mm.FontSize=20
mm.Items.add("Notifications:")
For Each Row as DataRow in mydt.Rows
  mm.Items.add(Row("name"))
Next

The code works great and I can see the list box but what I want is to style each listbox item. For example I want the first one with fontsize=24, fontbold, backcolor=black, font color white and the rest with fontsize=16

There is no listbox on the xaml, it is created and painted by code.

Any help or clue?

Thanks in advance

Upvotes: 0

Views: 967

Answers (1)

Clemens
Clemens

Reputation: 128060

You could explicitly create a ListBoxItem for every item and set all necessary properties. In C# it may look like shown below (sorry, i don't speak VB).

mm.Items.Add(new ListBoxItem
{
    Content = "Item 1",
    Foreground = Brushes.White,
    Background = Brushes.Black,
    FontSize = 24,
    FontWeight = FontWeights.Bold
});
mm.Items.Add(new ListBoxItem
{
    Content = "Item 2",
    FontSize = 16
});
...

Upvotes: 2

Related Questions