For example:I have a comboBox which has three items:AAAAA,BBBBB,CCCCC.Now the selected item is AAAAA, when I select BBBBB, selection changed event is fired. I want combobox show the current selected item (which now is BBBBB),but when messagebox show ,combobox still show AAAAA,just like the screen-shot below:
This is not what I want,I want ComboBox show BBBBB,and then the messagebox is popup. I didn't find any way to solve this problem .Can anybody help me ? Thanks!
CodePudding user response:
You can use the DropDownClosed event.
CodePudding user response:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox x:Name="cbItem" Height="20" Width="150" SelectionChanged="cbItem_SelectionChanged"></ComboBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
cbItem.Items.Add("AAAA");
cbItem.Items.Add("BBBB");
cbItem.Items.Add("CCCC");
}
private void cbItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cmb = sender as System.Windows.Controls.ComboBox;
var str = cmb.SelectedItem;
MessageBox.Show(str.ToString());
}
}
}
use selection changed event and get selected item from Combobox and print it