C# 如何从ComboBox事件中获取选定的listview项
我正在处理一个项目,需要在ListView中显示一个组合框,组合框使用双向模式绑定。每当组合框选择更改时,我需要触发一个事件,并从listview中获取所选组合框的所选项 每当触发组合框选择更改事件时,我需要选择此项,以便获得所选项 编辑:这是事件代码C# 如何从ComboBox事件中获取选定的listview项,c#,wpf,combobox,C#,Wpf,Combobox,我正在处理一个项目,需要在ListView中显示一个组合框,组合框使用双向模式绑定。每当组合框选择更改时,我需要触发一个事件,并从listview中获取所选组合框的所选项 每当触发组合框选择更改事件时,我需要选择此项,以便获得所选项 编辑:这是事件代码 private void ProductTypeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox com
private void ProductTypeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combo = e.OriginalSource as ComboBox;
ComboBoxItem cbItem = (ComboBoxItem) combo.SelectedItem;
string selected = cbItem.Content.ToString();
switch (selected)
{
case "Vente" :
var pro = this.ProductsToAddListView.SelectedItem;
break;
default:
MessageBox.Show("Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
break;
}
}
你要做的是遍历组合框的祖先,直到找到你想要的。下面的函数是一个通用版本,您要做的是使用ListViewItem作为类型T,使用combobox作为参数
private static T FindUIElementParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}`
私有静态T FindUIElementParent(UIElement元素),其中T:UIElement
{
UIElement父元素=元素;
while(父级!=null)
{
T correctlyTyped=父项为T;
if(correctlyTyped!=null)
{
返回正确类型;
}
parent=VisualTreeHelper.GetParent(parent)作为UIElement;
}
返回null;
}`
请添加您的代码您迄今为止所做的可能是@Kulasangar的复制品这与我的situation@redaa哦,对不起,我误解了,一旦你有了listviewitem,你就可以得到内容并播放它。