首页 > 解决方案 > WPF - 当用户点击复选框时如何获取treeviewitem

问题描述

我在 C# 后面的代码中定义了ItemTemplatefor TreeView。在那ItemTemplate,我在CheckBox其中定义了一个和一个标签。and 标签的TagandText属性与数据绑定。CheckBox

现在,我想TreeViewItem在用户单击 CheckBox 时获取该对象。我在 ' 上使用了 click 事件,CheckBox但该事件没有给我TreeViewItem' 对象

如果有人可以解释解决方案,我将不胜感激。

这是用 C# 编写的代码:

public static void FillTree()
{
    //assign treeview itemtemplate
    BIMExplorerUserControl.Instance.BimTreeView.ItemTemplate = GetHeaderTemplate();
    BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);

    foreach (var category in ViewModel.Instance.DefaultExplorerView)
    {
        BIMExplorerUserControl.Instance.BimTreeView.Items.Add(category);

    }
}



public static DataTemplate GetHeaderTemplate()
{
    //create the data template
    DataTemplate dataTemplate = new DataTemplate();

    //create stack pane;
    FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.Name = "parentStackpanel";
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    // Create check box
    FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
    checkBox.Name = "chk";
    checkBox.SetValue(CheckBox.NameProperty, "chk");
    checkBox.SetValue(CheckBox.TagProperty, new Binding());
    checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
    checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
    checkBox.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckedEvent));
    stackPanel.AppendChild(checkBox);


    // create text
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
    label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
    label.SetValue(TextBlock.ToolTipProperty, new Binding());

    stackPanel.AppendChild(label);


    //set the visual tree of the data template
    dataTemplate.VisualTree = stackPanel;

    return dataTemplate;

}


static void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    if (BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
    {
        foreach (var category in ViewModel.Instance.DefaultExplorerView)
        {
            TreeViewItem item = (TreeViewItem)BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.ContainerFromItem(category);
            if (item == null) continue;
            item.IsExpanded = false;
            if (item.Items.Count == 0)
            {
                foreach (var element in category.Elements)
                {
                    item.Items.Add(element);
                }
            }
        }
    }

}

private static void CheckedEvent(object sender, RoutedEventArgs e)
{
    CheckBox checkbox = (CheckBox)e.Source;
    var name = checkbox.Tag.ToString();

}

在此处输入图像描述

标签: c#wpfeventsdata-bindingtreeview

解决方案


您可以使用以下辅助方法来获取对可视树中CheckBox的父级的引用:TreeViewItem

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

用法:

private static void CheckedEvent(object sender, RoutedEventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    var name = checkbox.Tag.ToString();

    TreeViewItem tvi = FindParent<TreeViewItem>(checkbox);
    //...
}

推荐阅读