簡體   English   中英

如何在 wpf 的 comboBox 中顯示新選定項目后顯示對話框?

[英]How to show dialog after new selecteditem is shown in comboBox in wpf?

例如:我有一個 comboBox 有三個項目:AAAAA,BBBBB,CCCCC。現在選擇的項目是 AAAA,當我 select BBBBB 時,選擇更改事件被觸發。 我想要 combobox 顯示當前選定的項目(現在是 BBBBB),但是當消息框顯示時,combobox 仍然顯示 AAAA,就像下面的屏幕截圖:

舊商品為 AAAAA,新商品為 BBBBB

這不是我想要的,我想要 ComboBox 顯示 BBBBB,然后彈出消息框。 我沒有找到任何方法來解決這個問題。有人可以幫助我嗎? 謝謝!

您可以使用DropDownClosed事件。

<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());
        }
    }
}

使用選擇更改事件並從 Combobox 獲取所選項目並打印

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM