What is the best way to bind Tag Attribute of ChildButton in TreeView(in the code Tag="{Binding ParentData}"
to ParentData property of class TreeParent?
<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>
And the .cs code behind xaml file looks like this:
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; }
}
Try this:
<Button x:Name="ChildButton" ...
Tag="{Binding DataContext.ParentData,
RelativeSource={RelativeSource AncestorType=TreeViewItem, AncestorLevel=2}}" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.