WPF C# ComboBox 选中事件:使用 SelectionChanged 实现
在 WPF 中,ComboBox 的选中事件可以通过 SelectionChanged 事件来实现。例如:
<ComboBox SelectionChanged='ComboBox_SelectionChanged'>
<ComboBoxItem Content='Item 1' />
<ComboBoxItem Content='Item 2' />
<ComboBoxItem Content='Item 3' />
</ComboBox>
在代码中,可以定义 SelectionChanged 事件的处理程序:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
ComboBoxItem selectedItem = comboBox.SelectedItem as ComboBoxItem;
MessageBox.Show('You selected ' + selectedItem.Content.ToString());
}
在这个处理程序中,我们首先将 sender 转换为 ComboBox,然后获取选中的 ComboBoxItem,并显示一个消息框来显示选中的项。
原文地址: https://www.cveoy.top/t/topic/kf64 著作权归作者所有。请勿转载和采集!