将 TreeView 中 ChildButton 的标记属性绑定Tag="{Binding ParentData}"到类 TreeParent 的 ParentData 属性的最佳方法是什么?<TreeView> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:TreeParent}" ItemsSource="{Binding Members}"> <Button x:Name="ParentButton" Text="{Binding ParentName}"> </Button> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type local:TreeChild}" ItemsSource="{Binding Members}"> <Button x:Name="ChildButton" Text="{Binding ChildName}" Tag="{Binding ParentData}"> </Button> </DataTemplate> </TreeView.Resources></TreeView>xaml 文件后面的 .cs 代码如下所示:public class TreeParent{ public string ParentName { get; set; } public SomeDataClass ParentData { get; set; } public ObservableCollection<TreeChild> Members { get; set; }}public class TreeChild{ public string ChildName { get; set; }}
1 回答
当年话下
TA贡献1890条经验 获得超9个赞
尝试这个:
<Button x:Name="ChildButton" ... Tag="{Binding DataContext.ParentData, RelativeSource={RelativeSource AncestorType=TreeViewItem, AncestorLevel=2}}" />
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报
0/150
提交
取消