c# - WPF - 当用户点击复选框时如何获取treeviewitem
问题描述
我在 C# 后面的代码中定义了ItemTemplate
for TreeView
。在那ItemTemplate
,我在CheckBox
其中定义了一个和一个标签。and 标签的Tag
andText
属性与数据绑定。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();
}
解决方案
您可以使用以下辅助方法来获取对可视树中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);
//...
}
推荐阅读
- python - 找不到 jsonschema 的包元数据
- sql - 基于 jsonb 子属性过滤掉的 Postgresql 查询
- c# - 使用 Serenity.is temp 过滤用户表的 LookUpEditor
- java - 使用 Lambda 将 CloudWatch Logs 导出到 S3 使用 JAVA 作为运行时语言
- r - 因子和虚拟变量的比例
- jquery - 如何将变量从模板传递到视图?
- tableau-api - Tableau - 严格显示前 N 个
- docker - 如何使用来自 k8s 的容器自定义卷
- python - 缺少类别的 Python sklearn one-hot 编码
- amazon-web-services - AWS JAVA SDK - 我可以使用在同一个 aws 账户中运行的 aws sdk 以编程方式访问其他服务,而无需硬编码凭证吗?