2

I have this ListBox:

<ListBox x:Name="PlaylistList" AlternationCount="2" SelectionChanged="DidChangeSelection">
        <ListBox.GroupStyle>
            <GroupStyle />
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And i want to add option to add new item to the ListBox. And i want to do it by adding a TextBox to the ListBox when the user press a Button and then the user press enter and add the text in the TextBox to the ListBox.

I try to add text box to the listbox but i can add only one type of ListBox.ItemTemplate, how can i handle it?

3 Answers 3

2

Updated to add textbox inside Listbox:

To add new item into ListBox, in Button Click code-behind do:

TextBox TextBoxItem = new TextBox();
// set TextBoxItem properties here
PlaylistList.Items.Add(TextBoxItem);
Sign up to request clarification or add additional context in comments.

1 Comment

But how i add a new item that will be a textbox
0

Your ListBox:

<ListBox
    Name="MyListBox"
    ItemsSource="{Binding Path=Reason}"
    DisplayMemberPath="Description"/>

Make a ObversableCollection for the Items of the ListBox

public ObservableCollection<IdObject> items
{
    get { return (ObservableCollection<IdObject>)GetValue(ApplicationsProperty); }
    set { SetValue(ApplicationsProperty, value); }
}

items = new ObservableCollection<IdObject>();

My ID-Object in this case has the following properties:

private int _id = 0;
private string _description = "";

Bind the collection to the ListBox:

MyListBox.ItemsSource = items;

Then make a TextBox with a Button, and an event for pressing the button, where you add the text to the observable collection:

items.Add(new IdObject(someId, TextBox.Text));

The ListBox will update, when the ObservableCollection is changed

3 Comments

But how i add the Text box to be one of the Items?
So you want the TextBox to be inside the ListBox?
Sorry, i don't know iTunes, post a link where such a box is shown
0

Write this code when your button is clicked so that the textbox text will be added to the listbox.

private void addEventButton_Click(object sender, EventArgs e)
    {


        // Adds events to listbox. 
       if (this.titleTextBox.Text != "")
        {
            listBox1.Items.Add(this.titleTextBox.Text); 
            listBox2.Items.Add(this.titleTextBox.Text);
            this.titleTextBox.Focus();
            this.titleTextBox.Clear();

         }
      }

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.