WPF ComboBox SelectionChanged和DropDownClosed事件不起作用

时间:2018-11-28 10:59:06

标签: wpf events combobox selectionchanged

我试图通过将方法链接到WPF中Combobox的Selection Change事件和DropDownClosed事件来调用方法,但是当我更改组合框中的项目时,它没有调用它应该使用的函数(在我的情况下为OnMyComboBoxChanged1和OnMyComboBoxChanged2) 。

  

MainWindow.xaml.cs

 public partial class MainWindow : Window
    {
        public List<string > NameOfPerson { get; set; }
        public string SelectedComboBoxItem { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            NameOfPerson = new List<string>();
            NameOfPerson.Add("Ram");
            NameOfPerson.Add("Sita");
            NameOfPerson.Add("Hari");
            NameOfPerson.Add("Kumar");
            NameOfPerson.Add("Jay");
            NameOfPerson.Add("Bikash");
            MyComboBox.ItemsSource = NameOfPerson;
           this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged1);
           this.MyComboBox.DropDownClosed += new System.EventHandler(OnMyComboBoxChanged2);
        }

        private void OnMyComboBoxChanged1(object sender, SelectionChangedEventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.Text;
        }
        private void OnMyComboBoxChanged2(object sender, System.EventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.Text;
        }

    }

  

XAML

 <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <Label Content="Combobox"/>
            <ComboBox x:Name="MyComboBox" Margin="50,0,0,0" Width="80"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
            <Label Content="The selected item is : "/>
            <Label Content="{Binding SelectedComboBoxItem}"/> 
        </StackPanel>
       
    </StackPanel>

感谢您的帮助

4 个答案:

答案 0 :(得分:1)

我已经尝试过了,方法被调用了。问题可能是您使用了错误的属性来检索所选项目。尝试以下方法:

SelectedComboBoxItem = this.MyComboBox.SelectedItem as string;

答案 1 :(得分:1)

标签的内容不会更新,因为没有内容告诉它要更新-标准C#属性没有自动通知。

您需要为SelectedComboBoxItem属性实现INotifyPropertyChanged,甚至最好切换到MVVM设计模式。

替代方法是使用直接数据绑定

<Label Content="{Binding ElementName="MyComboBox", Path=SelectedItem}" />

之所以可行,是因为控件的属性(通常是DependencyProperties确实可以提供更改通知)。

评论后编辑

请发布minimal, complete, and verifiable example,然后...以下代码对我来说很好。

public MainWindow()
{
    InitializeComponent();

    var NameOfPerson = new List<string>();
    NameOfPerson.Add("Ram");
    NameOfPerson.Add("Sita");
    NameOfPerson.Add("Hari");
    NameOfPerson.Add("Kumar");
    NameOfPerson.Add("Jay");
    NameOfPerson.Add("Bikash");
    MyComboBox.ItemsSource = NameOfPerson;

    MyComboBox.SelectionChanged += (s,e) => MyComboBoxOnSelectionChanged();
}

private void MyComboBoxOnSelectionChanged()
{
    SelectedComboBoxItem = MyComboBox.SelectedItem.ToString();
    Debugger.Break(); // proof that the event handler is being called
}

答案 2 :(得分:1)

您需要做两件事。

  1. 您应该使用属性字段SelectedComboBoxItem的后备字段实现INotifyPropertyChanged接口。

  2. 您需要像这样将DataContext设置为您的类。

    this.DataContext = this;

答案 3 :(得分:0)

下面是答案和工作代码,以防万一有人需要

  

MainWindow.xaml.cs

using System.ComponentModel;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Diagnostics;

namespace Combobox
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<string> NameOfPerson { get; set; }
        private string _SelectedComboBoxItem;
        public string SelectedComboBoxItem
        {
            get
            {
                return _SelectedComboBoxItem;
            }

            set
            {
                if (_SelectedComboBoxItem == value)
                    return;
                _SelectedComboBoxItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedComboBoxItem)));
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            NameOfPerson = new List<string>();
            NameOfPerson.Add("Ram");
            NameOfPerson.Add("Sita");
            NameOfPerson.Add("Hari");
            NameOfPerson.Add("Kumar");
            NameOfPerson.Add("Jay");
            NameOfPerson.Add("Bikash");
            MyComboBox.ItemsSource = NameOfPerson;
            this.MyComboBox.SelectionChanged += OnMyComboBoxChanged1;
            this.MyComboBox.DropDownClosed += OnMyComboBoxChanged2;
        }


        private void OnMyComboBoxChanged1(object sender, SelectionChangedEventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.SelectedItem as string;
        }
        private void OnMyComboBoxChanged2(object sender, System.EventArgs e)
        {
            SelectedComboBoxItem = this.MyComboBox.Text;
            Debugger.Break();
        }

    }

}

  

XAML

<Window x:Class="Combobox.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:Combobox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="300">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <Label Content="Combobox"/>
            <ComboBox x:Name="MyComboBox" Margin="50,0,0,0" Width="80"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
            <Label Content="The selected item is : "/>
            <Label Content="{Binding SelectedComboBoxItem}"  />
        </StackPanel>
       
    </StackPanel>
</Window>