2

我有以下 .xaml:

<TreeView ItemsSource="{Binding EntityInstanceGroupings}">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TreeViewItem ItemsSource="{Binding EntityInstances}">
                <TreeViewItem.HeaderTemplate>
                    <DataTemplate>
                        <DockPanel LastChildFill="True">
                            <TextBlock Text="{Binding ...}" />
                            <Button Content="Add" DockPanel.Dock="Right" VerticalContentAlignment="Top">
                                ...
                            </Button>
                        </DockPanel>
                    </DataTemplate>
                </TreeViewItem.HeaderTemplate>
                <TreeViewItem.ItemTemplate>
                    ...
                </TreeViewItem.ItemTemplate>
            </TreeViewItem>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

在线:

<TextBlock Text="{Binding ...}" />

我正在尝试绑定到 EntityInstanceGroupings 视图模型中的属性“GroupName”。我想不出这样做(如果可能的话)。任何帮助将不胜感激 :)

4

3 回答 3

3

您应该能够使用RelativeSource.

<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},
    Path=DataContext.GroupName}" />
于 2012-09-18T23:12:21.553 回答
0

虽然我试图做的事情已经实现,但我确实找到了更好的解决方案来实现我想要的。

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="entityTemplate">
        <TextBlock Text="{Binding LayoutName}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="groupingTemplate" ItemsSource="{Binding EntityInstances}" ItemTemplate="{StaticResource entityTemplate}">
        <DockPanel LastChildFill="True">
            <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
            <Button Content="Add" DockPanel.Dock="Right" VerticalContentAlignment="Top">
                ...
            </Button>
        </DockPanel>
    </HierarchicalDataTemplate>    
</UserControl.Resources>

<!-- Instantiate the TreeView control -->
<TreeView ItemsSource="{Binding EntityInstanceGroupings}" ItemTemplate="{StaticResource groupingTemplate}" />
于 2012-09-19T20:01:35.427 回答
0

如果我可以按照您的逻辑,您TreeView将获得EntityInstance group的列表。TreeViews rootNodes 是这些组。在TreeView.ItemTemplate你想显示每个组的名称。

在这种情况下DataContext, theTreeViewItem和 theTextBox应该是一组 EntityInstances

这意味着您只需将Binding直接设置为道具:

<TextBlock Text="{Binding GroupName}" />
于 2012-09-19T06:18:46.583 回答