获取ComboBoxItem所在的ComboBox [英] Get the ComboBox that a ComboBoxItem resides in

查看:34
本文介绍了获取ComboBoxItem所在的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个ComboBoxItem所在的ComboBox.

I need to find the ComboBox that a ComboBoxItem resides in.

在后面的代码中,当单击ComboBoxItem时,我捕获了一个事件,但我不知道特定ComboBoxItem属于几个ComboBox中的哪一个.如何找到组合框?

In codebehind I catch an event when a ComboBoxItem is clicked, but I don't know which one of several ComboBoxes that the specific ComboBoxItem belongs to. How do I find the ComboBox?

通常,您可以使用LogicalTreeHelper.GetParent()并从ComboBoxItem遍历逻辑树以找到ComboBox.但这仅在将ComboBoxItems手动添加到ComboBox时才有效,而不是在通过数据绑定将这些项应用于ComboBox时才起作用.使用数据绑定时,ComboBoxItems没有将ComboBox作为逻辑父级(我不知道为什么).

Normally you can use LogicalTreeHelper.GetParent() and traverse up the logical tree from the ComboBoxItem to find the ComboBox. But this only works if the ComboBoxItems are added to the ComboBox manually, not when the items are applied to the ComboBox with databinding. When using databinding, the ComboBoxItems do not have the ComboBox as a logical parent (I don't understand why).

有什么想法吗?

更多信息:

下面是一些重构我的问题的代码(不是我的实际代码).如果将数据绑定ComboBoxItems更改为手动设置(在XAML中),则变量"comboBox"将设置为正确的ComboBox.现在,comboBox只为空.

Below is some code reconstructing my problem (not my actual code). If I would change from databinding the ComboBoxItems to setting them manually (in the XAML), the variable "comboBox" would be set to the correct ComboBox. Now comboBox is only null.

XAML:

<ComboBox Name="MyComboBox" ItemsSource="{Binding Path=ComboBoxItems, Mode=OneTime}" />

CodeBehind:

CodeBehind:

public MainWindow()
{
    InitializeComponent();

    MyComboBox.DataContext = this;
    this.PreviewMouseDown += MainWindow_MouseDown;
}

public BindingList<string> ComboBoxItems
{
    get
    {
        BindingList<string> items = new BindingList<string>();
        items.Add("Item E");
        items.Add("Item F");
        items.Add("Item G");
        items.Add("Item H");
        return items;
    }
}

private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
    DependencyObject clickedObject = e.OriginalSource as DependencyObject;
    ComboBoxItem comboBoxItem = FindVisualParent<ComboBoxItem>(clickedObject);
    if (comboBoxItem != null)
    {
        ComboBox comboBox = FindLogicalParent<ComboBox>(comboBoxItem);
    }
}

//Tries to find visual parent of the specified type.
private static T FindVisualParent<T>(DependencyObject childElement) where T : DependencyObject
{
    DependencyObject parent = VisualTreeHelper.GetParent(childElement);
    T parentAsT = parent as T;
    if (parent == null)
    {
        return null;
    }
    else if (parentAsT != null)
    {
        return parentAsT;
    }
    return FindVisualParent<T>(parent);
}

//Tries to find logical parent of the specified type.
private static T FindLogicalParent<T>(DependencyObject childElement) where T : DependencyObject
{
    DependencyObject parent = LogicalTreeHelper.GetParent(childElement);
    T parentAsT = parent as T;
    if (parent == null)
    {
        return null;
    }
    else if(parentAsT != null)
    {
        return parentAsT;
    }
    return FindLogicalParent<T>(parent);
}

推荐答案

这可能是您正在寻找的东西:

This is probably what you are looking for:

var comboBox = ItemsControl.ItemsControlFromItemContainer(comboBoxItem) as ComboBox;

我喜欢方法名的描述性.

I love how descriptive that method-name is.

在旁注中,可以在属性

On a side-note, there are some other useful methods which can be found in the property ItemsControl.ItemContainerGenerator which let you get the container associated with the templated data and vice versa.

另一方面,通常不要使用它们中的任何一个,而应实际使用数据绑定.

On another side-note, you usually should not be using any of them and instead use data-binding actually.

这篇关于获取ComboBoxItem所在的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆