从嵌套的DataTemplate绑定到HierarchicalDataTemplate DataContext

时间:2014-09-05 08:01:17

标签: wpf treeview hierarchical-data

我的树视图有2级嵌套级别。

<TreeView ItemsSource="{Binding Project.Tables}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type TableModel}" ItemsSource="{Binding Columns}">
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type ColumnModel}">
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

我的表格绑定源:

public class ProjectModel : PropertyChangedBase
{
    private BindableCollection<TableModel> _tables;

    public BindableCollection<TableModel> Tables
    {
        get { return _tables; }
        set
        {
            if (_tables != value)
            {
                _tables = value;
                NotifyOfPropertyChange();
            }
        }
    }
}

public class TableModel
{
    public BindableCollection<ColumnModel> Columns { get; set; }

    public bool IsSelected
    {
        get;
    }
}

我尝试使用下一个绑定:

 <DataTemplate DataType="{x:Type projectModels:ColumnModel}">
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=DataContext.IsSelected }" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource GreyBrush3}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>

            <TextBlock Text="{Binding ColumnName}">
            </TextBlock>
        </StackPanel>
    </DataTemplate>

Inside DataTemplate我希望绑定到HierarchicalDataTemplate的DataContext。我已经尝试了不同的AncestorLevels并且它没有用,任何想法我该怎么办?

0 个答案:

没有答案