【问题标题】:How to show dialog after new selecteditem is shown in comboBox in wpf?wpf的组合框中显示新的选定项后如何显示对话框?
【发布时间】:2022-01-24 04:04:01
【问题描述】:

例如:我有一个包含三个项目的组合框:AAAAA、BBBBB、CCCCC。现在选定的项目是 AAAAA,当我选择 BBBBB 时,会触发选择更改事件。我希望组合框显示当前选定的项目(现在是 BBBBB),但是当消息框显示时,组合框仍然显示 AAAAA,就像下面的屏幕截图:

这不是我想要的,我想要 ComboBox 显示 BBBBB,然后弹出消息框。我没有找到解决这个问题的任何方法。有人可以帮助我吗?谢谢!

【问题讨论】:

    标签: c# wpf combobox


    【解决方案1】:

    您可以使用DropDownClosed 事件。

    【讨论】:

    • 它有效!非常感谢!
    • hi,jesse,如果我使用mvvm,绑定所选项目到ViewModel,当SelectsItem改变时,没有DropDownClose事件,我如何处理这个问题? span>
    • @FranklinLee:如果您使用鼠标选择组合框,则应该始终有一个 dropdownclose 事件。您是说您正在以编程方式更改选定项吗?当你说when selecteditem changed时,项目是如何改变的?
    • :我说清楚了。我们都知道 Combbox 有 ItemsSource 和 SelectedItem 属性。因此,如果我将 ItemsSource 绑定到 Observable(我们称之为 Student),并将 SelectedItem 绑定到 T 的实例(我们称之为 Student),那么我可以通过更改 Student 来更改组合框 selectedItem。在这种情况下,如果我想在新选择的项目显示在组合框中后显示一个对话框,我该怎么做?
    • @FranklinLee:我没有看到问题,为什么在你更改学生后不显示对话框?如果您提出另一个问题并显示您正在使用的代码,可能会更容易理解您的问题。
    【解决方案2】:
    <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());
            }
        }
    }
    

    使用选择更改事件并从组合框中获取所选项目并打印它

    【讨论】:

    • 谢谢,但这不是我想要解决的问题。
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多