I want to populate a listbox after inputting text into a text box and clicking Submit. Seems simple I know, but I’m new to Data Binding and WPF…
Here’s my code so far… I don’t know if the XAML is correct, and of course I have nothing in the event code behind… any help would be appreciated.
XAML:
<ListBox ItemsSource="{Binding ElementName=accountaddTextBox, Path=SelectedItem.Content, Mode=OneWay, UpdateSourceTrigger=Explicit}" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />
Code behind:
private void okBtn_Click(object sender, RoutedEventArgs e)
{
}
Your current binding is telling the
ListBoxto find an object namedaccountaddTextBox, and bind to itsSelectedItem.Content. I am assuming thataccountaddTextBoxis aTextBox, andSelectedItemis not a valid property onTextBox, so your binding is invalid.It would be far better to bind your ListBox to an
ObservableCollection<string>that is located in your code-behind orViewModel, and have your button add a new object to that collection. Since it is anObservableCollection, the UI will automatically updateFor example,