Hierarchical Datatemplate绑定到DataType Not DataContext

时间:2012-02-09 18:40:17

标签: wpf xaml data-binding

我有一个带有Tree的usercontrol,command属性绑定到DataType而不是DataContext。

如何重定向绑定以转到DataContext而不是DataType?出于好奇,如何绑定到UserControl的DataContext而不是Tree的DataContext?

以下是相关代码:

<HierarchicalDataTemplate 
            DataType="{x:Type viewModel:UsersViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserName}">
                    <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                                <MenuItem Header="Delete"/>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

它似乎绑定到UsersViewModel而不是DataContext(AllUsersViewModel)。

这是整个XAML以防万一:

 <Grid Width="150">
<TreeView ItemsSource="{Binding Users}" DataContext="{Binding allUsersViewModel}">
    <TreeView.ItemContainerStyle>
        <!-- 
    This Style binds a TreeViewItem to a TreeViewItemViewModel. 
    -->
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>    
        <ContextMenu x:Key="CategoryMenu">
            <MenuItem Header="Add Subcategory" Command="New">
            </MenuItem>

            <MenuItem Header="Remove Category" Command="Delete">
            </MenuItem>
        </ContextMenu>

        <HierarchicalDataTemplate 
            DataType="{x:Type viewModel:UsersViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding UserName}">
                    <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding UserName}"/>
                                <MenuItem Header="Delete"/>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate 
            DataType="{x:Type viewModel:PermissionCategoryViewModel}" 
            ItemsSource="{Binding Children}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding PermissionCategoryName}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type viewModel:PermissionViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding PermissionName}" />
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>
</Grid> 

感谢您的帮助!

修改

嗯,我尝试了一些东西,但它没有成功=(。什么都没发生

我应该提到我的MainWindow有DataContext和UserControl通过将UserControl放在其中来继承它。

<Window.DataContext>
    <viewModel:MainViewModel/>
</Window.DataContext>

我尝试了这些不同的东西

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.EditCommand}"

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AllUsers}}, Path=DataContext.EditCommand}"

最后

Command="{Binding PlacementTarget.DataContext.EditCommand, 
                      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"

1 个答案:

答案 0 :(得分:3)

您可以使用RelativeSource绑定查找TreeView,然后绑定到它的DataContext

Command="{Binding Path=DataContext.EditCommand,
    RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}"

如果您想要绑定到UserControl,可以使用相同类型的绑定:

RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}

请注意,RelativeSource绑定将返回对UI对象的引用,而不是DataContext,因此如果要绑定到DataContext中的某些内容,则必须指定{{1} }}