【问题标题】:How to get the selected listview item from ComboBox Event如何从 ComboBox 事件中获取选定的列表视图项
【发布时间】:2014-08-13 14:25:24
【问题描述】:

我正在做一个项目,我需要在 ListView 中显示一个组合框,组合框是使用双向模式绑定的。每当 Combobox 选择发生变化时,我都需要触发一个事件,并从列表视图中获取选定 ComboBox 的选定项。

每当触发组合框选择更改事件时,我都需要选择此项目,以便我可以获取所选项目。

编辑:这是事件代码。

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;     
        }
    }

【问题讨论】:

标签: c# wpf combobox


【解决方案1】:

您想要做的是遍历组合框的祖先,直到找到您想要的。下面的函数是一个通用版本,你要做的就是使用 ListViewItem 作为类型 T 和你的组合框作为参数。

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;
}`

【讨论】:

  • @redaa 哦,对不起,我误解了,一旦你有了 listviewitem,你就可以获取内容并投射它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多