2

How do we add new items to a ListBox control in a Windows Metro style application at run-time?

I come from WinForms, so as you could imagine, I'm pretty confused right now.

I have the following:

public class NoteView
{
   public string Title { get; set; }
   public string b { get; set; }
   public string c { get; set; }
}

and then:

List<NoteView> notes = new List<NoteView>();

protected void Button1_Click(object sender, RoutedEventArgs e)
{
   notes.Add(new NoteView {
      a = "text one",
      b = "whatevs",
      c = "yawns"
   });

   NotesList.ItemsSource = notes;
}

Which is useless. It does nothing. Also, there is nothing in the Output window. No errors, no exceptions; nothing.

So, then I tried directly adding to the ListBox:

NotesList.Items.Add("whatever!");

Again, nothing happened. So then I tried adding UpdateLayout(); but that didn't help either.

Anybody know what's up with that?

How do we add new items to a XAML ListBox?

Update:

        <ListBox Name="NotesList" Background="WhiteSmoke">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title, Mode=TwoWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
2

3 Answers 3

1

YOu will have to do it a bit differently, you cannot just asign all the properties to listBox. So create this kind of class:

 public class NoteView
{
    public string Item { get; set; }
    public int Value { get; set; }
}

And this is then the code inside button click event:

        List<NoteView> notes = new List<NoteView>();
        notes.Add(new NoteView { Item = "a", Value = 1 });
        notes.Add(new NoteView { Item = "b", Value = 2 });
        notes.Add(new NoteView { Item = "c", Value = 3 });

        listBox1.DataSource = notes;
        listBox1.DisplayMember = "Item";
        listBox1.ValueMember = "Value";

-- Else if you mean to use the same class as you created, then you can do it like:

        List<NoteView> notes = new List<NoteView>();
        notes.Add(new NoteView
        {
            a = "text one",
            b = "whatevs",
            c = "yawns"
        });

        listBox1.Items.Add(notes[0].a);
        listBox1.Items.Add(notes[0].b);
        listBox1.Items.Add(notes[0].c);
Sign up to request clarification or add additional context in comments.

2 Comments

There is no DataSource available to use for ListBox in Metro apps.
Second option shows "Value does not fall within expected range" exception.
0
List<NoteView> notes = new List<NoteView>();

protected void Button1_Click(object sender, RoutedEventArgs e)
{
   notes.Add(new NoteView {
      a = "text one",
      b = "whatevs",
      c = "yawns"
   });
NotesList.DisplayMember = "a";
        NotesList.ValueMember = "b";
   NotesList.ItemsSource = notes;
}

4 Comments

Sorry, I've already tried that way, too. But thank you for your help.
Are you using WPF or Winforms. If WPF then it is different way to bind .
Xaml in Metro style application; refer to question tags. You don't get XAML in WinForms.
.net winform don't have xaml only wpf have the xaml
0

I managed to figure out how to do it:

notes.Insert(0, new NoteView { a = "Untitled note", b = "", c = "" });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.