WPF ComboBox SelectionChanged 和 DropDownClosed 事件不起作用
WPF ComboBox SelectionChanged and DropDownClosed events is not working
我试图通过将方法链接到 WPF 中组合框的选择更改事件和 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>
感谢您的帮助
我试过了,调用了方法。问题可能是您使用了错误的 属性 来检索所选项目。试试这个:
SelectedComboBoxItem = this.MyComboBox.SelectedItem as string;
标签的内容不会更新,因为没有任何内容告诉它要更新 - 标准 C# 属性没有自动通知。
您需要实施 INotifyPropertyChanged
for your SelectedComboBoxItem property, or even better switch to the MVVM
设计模式。
替代方法是使用直接数据绑定
<Label Content="{Binding ElementName="MyComboBox", Path=SelectedItem}" />
这是可行的,因为控件的属性(通常)DependencyProperties
确实提供了更改通知。
评论后编辑
请post一个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
}
你需要做两件事。
您应该为您的 属性 SelectedComboBoxItem 实现 INotifyPropertyChanged 带有支持字段的接口。
您需要像这样将 DataContext 设置为您的 class。
this.DataContext=这个;
下面是答案和工作代码,以备不时之需
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>
我试图通过将方法链接到 WPF 中组合框的选择更改事件和 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>
感谢您的帮助
我试过了,调用了方法。问题可能是您使用了错误的 属性 来检索所选项目。试试这个:
SelectedComboBoxItem = this.MyComboBox.SelectedItem as string;
标签的内容不会更新,因为没有任何内容告诉它要更新 - 标准 C# 属性没有自动通知。
您需要实施 INotifyPropertyChanged
for your SelectedComboBoxItem property, or even better switch to the MVVM
设计模式。
替代方法是使用直接数据绑定
<Label Content="{Binding ElementName="MyComboBox", Path=SelectedItem}" />
这是可行的,因为控件的属性(通常)DependencyProperties
确实提供了更改通知。
评论后编辑
请post一个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
}
你需要做两件事。
您应该为您的 属性 SelectedComboBoxItem 实现 INotifyPropertyChanged 带有支持字段的接口。
您需要像这样将 DataContext 设置为您的 class。
this.DataContext=这个;
下面是答案和工作代码,以备不时之需
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>