I'm trying to make it so that a user can choose an item from a combo box, press a button and the selected item will be added to a listbox. The problem is that once I make any choice and press the button - the listbox seems to glitch out and the horizontal scrolling slider pops up and takes up the entire space (don't even know if the item gets added).
XAML code:
<Label Name="UserEntryLabel" Content="Your chosen entries:" Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="15" FontWeight="Bold" Foreground="DarkBlue" Margin="0,0,10,8" Padding="0,0,0,0"/>
<ListBox Name="UserEntryBox" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="120" Height="20" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="0,0,0,8"/>
<Label Name="NewEntryLabel" Content="Add new entry:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="Bold" Foreground="DarkBlue" Margin="0,8,6,0" Padding="0,0,0,0"/>
<ComboBox Name="NewEntryBox" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120" Height="20" Margin="0,8,0,0">
<ComboBoxItem IsSelected="True">Entry1</ComboBoxItem>
<ComboBoxItem>Entry2</ComboBoxItem>
<ComboBoxItem>Entry3</ComboBoxItem>
<ComboBoxItem>Entry4</ComboBoxItem>
</ComboBox>
<Button Name="NewEntryButton" Content="Add" Grid.Row="2" Grid.Column="2" HorizontalAlignment="left" VerticalAlignment="Top" Width="90" Height="20" Margin="0,8,0,0" Click="NewChestButtonClick"/>
CS code:
private void NewCEntryButtonClick(object sender, RoutedEventArgs e)
{
AddNewEntry();
}
private void AddNewEntry()
{
ListBoxItem TempItem = new ListBoxItem();
string EntryType = NewEntryBox.SelectedItem.ToString();
TempItem.Content = EntryType;
UserEntryBox.Items.Add(TempItem);
}
Your problem is that you weren't adding the value in the combobox but the whole item reference too :
System.Windows.Controls.ComboBoxItem: Entry1
If you change how you add the value of the combobox you can see it appear in the listbox :
private void AddNewEntry()
{
UserEntryBox.Items.Add(NewEntryBox.Text);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.