XAML 数据绑定到另一个类

将 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; }

}


catspeake
浏览 110回答 1
1回答

当年话下

尝试这个:<Button&nbsp;x:Name="ChildButton"&nbsp;...&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tag="{Binding&nbsp;DataContext.ParentData,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RelativeSource={RelativeSource&nbsp;AncestorType=TreeViewItem,&nbsp;AncestorLevel=2}}"&nbsp;/>
打开App,查看更多内容
随时随地看视频慕课网APP