Reputation: 7890
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?
Upvotes: 2
Views: 17034
Reputation:
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();
}
}
Upvotes: 0
Reputation: 698
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);
Upvotes: 2
Reputation: 2343
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
Upvotes: 0